简体   繁体   English

Wpf:Prism + MVVM:更改模块/视图

[英]Wpf: Prism + MVVM: Changing Modules/Views

I'm developing a WPF application using MVVM + Prism. 我正在使用MVVM + Prism开发WPF应用程序。 The Shell is devided into two regions: Menu + MainScreenArea. 外壳分为两个区域:Menu + MainScreenArea。

The Menu includes navigation: Search Entity, Add Entity, Edit Entity. 菜单包括导航:搜索实体,添加实体,编辑实体。 And Basically the mainScreenArea should load the appropriate module/View. 基本上,mainScreenArea应该加载适当的模块/视图。 If Search Entity is chosen in the Menu Region, The mainScreenArea should display the SearchEntity Module/View. 如果在菜单区域中选择了搜索实体,则mainScreenArea应显示搜索实体模块/视图。

I still haven't coded it, but I think I will create A module for each purpose: SearchEntityModule, AddEntityModule and etc. 我仍然没有编写代码,但是我想我将为每种目的创建一个模块:SearchEntityModule,AddEntityModule等。

Then the MainWorkArea will change modules on demand by the corresponding click on Menu Region. 然后,MainWorkArea将通过相应地单击菜单区域按需更改模块。

Now, how do I change between the modules in the MainScreenArea Region? 现在,如何在MainScreenArea Region中的模块之间进行切换? Should I load the nameOfModule to eventAggregator from MenuRegion and and MainScreenArea will get the name of screen from the aggregator? 我应该从MenuRegion将nameOfModule加载到eventAggregator,并且MainScreenArea将从聚合器获取屏幕名称吗?

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 http://msdn.microsoft.com/zh-CN/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. 就我而言,我有一个类似的shell ,它具有一个导航区域和一个主区域,并且我的功能分为多个模块。 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). 我的所有模块都向该导航区域添加了自己的导航视图(在模块初始化时,向导航区域add了自己的导航视图)。 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. 因此,基本上该模块具有3个可能的视图,可以将其放置到MainView中,关键步骤是将其添加到区域中并将其激活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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