Prism - How to import IRegionManager into ViewModel using MEF

How do we inject IRegionManager into ViewModel using MEF Container. I need to switch the view in my command ViewModel. Here's a quick summary of what I am doing. I have an object called Product, which is listed in one view (ProductListView). In this view, the user can select a Product and click the Edit button. This will switch the view and present a new View (ProductEditView). To activate another view, I need a link to the IRegionManager like this:

public class ProductListVM : NotificationObject { //The Product List View Model
    [Import]
    public IRegionManager RegionManager { get; set; }

    private void EditProduct() { //EditCommand fired from ProductListView
        IRegion mainContentRegion = RegionManager.Regions["MainRegion"];
        //Switch the View in "MainContent" region.
        ....
    }
}

      

The above code fails with a NullReferenceException on the RegionManager. This seems logical because the above View Model is built by WPF via the DataContext property in Xaml and DI does not come into play, so it has no way to import a RegionManager instance. How do I resolve the IRegionManager in this scenario.

+3


source to share


2 answers


The container instance can be exported to bootstrapper using the following

    container.ComposeExportedValue<CompositionContainer>(container);

      

Then in the view model, the IRegionManager instance can be imported using the code



    IServiceLocator serviceLocator = ServiceLocator.Current;
    CompositionContainer container = serviceLocator.GetInstance<CompositionContainer>();
    RegionManager = container.GetExportedValue<IRegionManager>();

      

However, referring to the View in the ViewModel is a violation of the MVVM pattern. But since I followed the article here to get to know Prism I had to go through the same thing. Also the article was in Silverlight and I had to find a way to import RegionManager to wpf which is a little different.

Regards, Nirvan.

+4


source


Try using [ImportingConstructor] like this:

public class ProductView : Window
{
  private IProductViewModel viewModel;


   [ImportingConstructor]
   public ProductView(IProductViewModel ViewModel)
   {
       this.viewModel = ViewModel;
       this.DataContext = this.viewModel;
   }
}


public class ProductViewModel: IProductViewModel, INotifyPropertyChanged
{
   private IRegionManager regionManager;
   private ICommand editUserCommand;

   [ImportingConstructor]
   public ProductViewModel(IRegionManager InsertedRegionManager)
   {
      this.regionManager = InsertedRegionManager;
      editUserCommand = new DelegateCommand(ExecuteEditUserCommand, CanExecuteEditUserCommand);
   }

   public ICommand EditUserCommand
   {
       get {return this.editUserCommnad;}
   }

   private bool CanExecuteEditUserCommand()
   {
      return true;
   }

   private void ExecuteEditUserCommand()
   {
      this.regionManager......
   }

      



}

+1


source







All Articles