I am trying to build a modular application with Prism / WPF
For the example i have 2 modules
- one that will provide gui to load images / parse folders
- one that will display the image after it has been processed by a service.
As displayed in the picture below, I want to have 2 instances of each visible at the same time in my application.
So in my main view I created 4 regions ie TopLeft, TopRight, BottomLeft, BottomRight and as I would like all this stuff to be modular / configurable in the future I have put in a json file a relation between the region and the view type, so I can register them dynamically in the OnInitialized of the corresponding prism module.
foreach (IConfigurationSection viewDeclaration in viewsDeclaration)
{
string id = _configuration[viewDeclaration.Path + ":id"] + "View";
if (typeof(ImageBrowserView).Name == id)
{
string region = _configuration[viewDeclaration.Path + ":region"];
//regionManager.RegisterViewWithRegion(region, typeof(ImageBrowserView));
ImageBrowserView view = new ImageBrowserView(_configuration[viewDeclaration.Path + ":msg_grp_id"]);
regionManager.Regions[region].Add(view, region + "View");
}
}
As you see I am creating the view object manually to initialize it with a parameter. This parameter
is used to indicate the origin of the image (like an id of the module that loaded it ) further in the application. So as I need it in the viewmodel, I created an interface for my viewmodels that need this info to assign the value from the view constructor.
public ImageBrowserView(string groupId)
{
InitializeComponent();
IViewModelWithGroupId vm = (IViewModelWithGroupId)this.DataContext;
vm.SetGroupId(groupId);
}
It is working but I am not sure if it is the correct solution as I am quite new to the WPF/MVVM world.
Any suggestions are really appreciated.
question from:
https://stackoverflow.com/questions/65915879/am-i-doing-it-correctly-do-deal-with-multiple-instances-of-same-view-in-mvvm-p 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…