简体   繁体   中英

Wpf: Prism + MVVM: Changing Modules/Views

I'm developing a WPF application using MVVM + Prism. The Shell is devided into two regions: Menu + MainScreenArea.

The Menu includes navigation: Search Entity, Add Entity, Edit Entity. And Basically the mainScreenArea should load the appropriate module/View. If Search Entity is chosen in the Menu Region, The mainScreenArea should display the SearchEntity Module/View.

I still haven't coded it, but I think I will create A module for each purpose: SearchEntityModule, AddEntityModule and etc.

Then the MainWorkArea will change modules on demand by the corresponding click on Menu Region.

Now, how do I change between the modules in the MainScreenArea Region? Should I load the nameOfModule to eventAggregator from MenuRegion and and MainScreenArea will get the name of screen from the aggregator?

Anyways, I'm new to this, so if I'm going in the wrong direction, please post me your suggestion. Thanks!

The prism documentation has a whole section on navigation. The problem with this question is that there are a number of different ways to go when loading modules on demand. I have posted a link that I hope leads you in the right direction. If it does, please mark this as answered. thank you

http://msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx

As has been said, there are a number of ways of accomplishing this. For my case, I have a similar shell that has a nav region and a main region, and my functionality is broken into a number of modules. My modules all add their own navigation view to that nav region (in the initialise of the module add their own nav-view to the nav-region). In this way my shell has no knowledge of the individual modules and the commands they may expose.

When a command is clicked the nav view model it belongs to does something like this:

/// <summary>
/// switch to the view given as string parameter
/// </summary>
/// <param name="screenUri"></param>
private void NavigateToView(string screenUri)
{
    // if there is no MainRegion then something is very wrong
    if (this.regionManager.Regions.ContainsRegionWithName(RegionName.MainRegion))
    {
        // see if this view is already loaded into the region
        var view = this.regionManager.Regions[RegionName.MainRegion].GetView(screenUri);
        if (view == null)
        {
            // if not then load it now
            switch (screenUri)
            {
                case "DriverStatsView":
                    this.regionManager.Regions[RegionName.MainRegion].Add(this.container.Resolve<IDriverStatsViewModel>().View, screenUri);
                    break;
                case "TeamStatsView":
                    this.regionManager.Regions[RegionName.MainRegion].Add(this.container.Resolve<ITeamStatsViewModel>().View, screenUri);
                    break;
                case "EngineStatsView":
                    this.regionManager.Regions[RegionName.MainRegion].Add(this.container.Resolve<IEngineStatsViewModel>().View, screenUri);
                    break;
                default:
                    throw new Exception(string.Format("Unknown screenUri: {0}", screenUri));
            }
            // and retrieve it into our view variable
            view = this.regionManager.Regions[RegionName.MainRegion].GetView(screenUri);
        }
        // make the view the active view
        this.regionManager.Regions[RegionName.MainRegion].Activate(view);
    }
}

So basically that module has 3 possible views it could place into the MainView, and the key steps are to add it to the region and to make it active.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM