Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

Am I doing it correctly do deal with multiple instances of same view in MVVM / Prism / WPF

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.

Application capture

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use navigation and navigation parameters for this, if you want to stick with regions at all. View model-first was a thing with Prism in the past, but nowadays they want everything to use the view model locator.

You can iterate through the configuration and navigate for each entry

_regionManager.RequestNavigate( entry.Region, entry.View, new NavigationParameters( entry.Parameters ) );

and have the view model parse the parameters (a dictionary essentially) and parametrize itself.

Alternatively, create the view models manually and expose them as a list of tabs and assign data templates to them (thus circumventing regions forcing your view models to wait for navigation parameters in an otherwise unnecessary, invalid state).

I'd always try to avoid making the view model known to the view, it just feels wrong. The same for constructor parameters for views - they tend to complicate things.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...