Passing parameters to ViewModels (Prism)

I cannot figure out how to pass parameters to my view modes from other views or view modes.

For example, I have View called Customers. There is a grid inside, and if you double-click on the grid, a new view should appear and allow you to edit the client data. But how will it be visible (Model), responsible for data editing, to know which client it should open if I can not pass any parameters inside?

An EventAggregator is out of the question because I obviously cannot create hundreds of eventargs, each for one view. And besides, this is a lousy decision.

So far, I could come up with:

CustomerDataView custView = new CustomerDataView(customerId, currentContext);
manager.Regions[RegionNames.Sidebar].AddAndActivate(custView);

What do you think of this particular decision? Is this usually done? What I don't like about this is the fact that I am losing the automatic injection of dependencies on Unity.

+3
source share
2 answers

Alternatively, you can upgrade Unity to the latest build, which supports parameter overrides.

MyType mt = container.Resolve<MyType>(
                      new ParameterOverride("customerId", customerId));

This is what I did. We found that the subcontainers maintained a circular reference to their parents and did not collect properly (memory leak), so we updated and decided to use this method.

+5
source

, M MVVM. . ( Unity ) . , Customer . , viewmodel .

, , CreateChildContainer(). :.

using (var childContainer = _container.CreateChildContainer())
{
    childContainer.RegisterInstance(customerId);
    var custView = childContainer.Resolve<CustomerDataView>();
    manager.Regions[RegionNames.Sidebar].AddAndActivate(custView);
}
+3

Source: https://habr.com/ru/post/1738683/


All Articles