Installing datacontext with prism

I am trying to create a module with PRISM and now I am setting the DataContext inside the view which means I can only use the Parameterless constructor but that means I cannot use dependency injection (I am using Unity) in the constructor I would like

If possible, I would not want neither view nor vm to know each other and would not want to use something like

private void RegisterServices()
{
    var employeeViewModel = new EmployeeViewModel();

    _container.RegisterType<IEmployeeViewModel, EmployeeViewModel>();
    _container.RegisterType<EmployeeView>();

    EmployeeView.Datacontext = employeeViewModel;
}

      

What will I register with EmployeeModule

Is this possible, or should I use code?

+3


source to share


2 answers


You can pass the interface ViewModel

to View

in the constructor. Thus, it View

only knows the interface ViewModel

and ViewModel

knows nothing about View

.

Ref.



public class EmployeeView : UserControl
{
    public EmployeeView(IEmployeeViewModel vm)
    {
         this.DataContext = vm; //// better to set the ViewModel in the Loaded method
    }
}

      

Refer to this blog post for several approaches to building MVVM.

+2


source


Prism documentation gives you the option

Often, you will find it helpful to define a controller or service class to coordinate the instantiation of view and view classes. This approach can be used with a dependency injection container like MEF or Unity, or when the view explicitly creates the required view of the model.

For my module, I will do the following: Create an interface for service inside the module

public interface ICustomModuleUiService
{
    void ShowMainView();
    void ShowExtraView();
}

      

Production implementation in the same module:



class CustomModuleUiService : ICustomModuleUiService
{
    private readonly IEventAggregator _eventAggregator;

    public CustomModuleUiService(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
    }

    public void ShowMainView()
    {
        var ddsViewModel = new DdsViewModel(_eventAggregator, this);
        DdsForm form = new DdsForm();
        form.DataContext = ddsViewModel;
        form.Show();
    }

    public void ShowExtraView()
    {
        //some code here
    }
}

      

And finally, the module code

[ModuleExport("DssModule", typeof(DssModuleImpl))]
public class DssModuleImpl : IModule
{
    private readonly IEventAggregator _eventAggregator;
    private ICustomModuleUiService _uiService;

    [ImportingConstructor]
    public DssModuleImpl(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        _uiService = new CustomModuleUiService(_eventAggregator);
    }

    public void Initialize()
    {
        _eventAggregator.GetEvent<OpenDdsFormEvent>().Subscribe(param => _uiService.ShowMainView());
    }
}

      

Using this approach I will get

  • ViewModel can be checked by module
  • I can dynamically change react to OpenDdsFormEvent by replacing the ICustomModuleUiService implementation
+1


source







All Articles