【问题标题】:Wpf: Prism + MVVM: Changing Modules/ViewsWpf:Prism + MVVM:更改模块/视图
【发布时间】:2014-06-12 05:20:47
【问题描述】:

我正在使用 MVVM + Prism 开发 WPF 应用程序。 Shell 分为两个区域:Menu + MainScreenArea。

菜单包括导航:搜索实体、添加实体、编辑实体。基本上 mainScreenArea 应该加载适当的模块/视图。如果在菜单区域中选择了搜索实体,则 mainScreenArea 应显示 SearchEntity 模块/视图。

我还没有编码,但我想我会为每个目的创建一个模块:SearchEntityModule、AddEntityModule 等。

然后MainWorkArea将通过相应的Menu Region点击改变模块。

现在,如何在 MainScreenArea 区域中的模块之间进行更改?我应该从 MenuRegion 将 nameOfModule 加载到 eventAggregator 并且 MainScreenArea 将从聚合器获取屏幕名称吗?

无论如何,我是新手,所以如果我走错了方向,请给我你的建议。谢谢!

【问题讨论】:

标签: c# wpf mvvm prism


【解决方案1】:

prism 文档有一整节关于导航。这个问题的问题在于,在按需加载模块时有许多不同的方法。我发布了一个链接,希望能引导您朝着正确的方向前进。如果是,请将其标记为已回答。谢谢

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

【讨论】:

    【解决方案2】:

    如前所述,有多种方法可以实现这一点。就我而言,我有一个类似的shell,它有一个导航区域和一个主区域,我的功能被分解为多个模块。我的模块都将它们自己的导航视图添加到该导航区域(在模块的初始化add 他们自己的导航视图到导航区域)。这样,我的 shell 就不知道各个模块以及它们可能暴露的命令。

    当一个命令被点击时,它所属的导航视图模型会做这样的事情:

    /// <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);
        }
    }
    

    所以基本上该模块有 3 个可能的视图可以放入 MainView,关键步骤是将其添加到区域并使其处于活动状态。

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 1970-01-01
      • 2012-11-14
      • 2011-01-02
      • 1970-01-01
      • 2012-07-17
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多