从 Prism 中的导航控件动态加载视图的最佳方法是什么

发布于 2024-08-10 06:44:56 字数 1191 浏览 3 评论 0原文

我已经使用菜单控件在我的应用程序中实现了导航,该菜单控件在单击菜单项时使用 EventAggregator 发布事件。如下所示,

this.eventAggregator.GetEvent<ViewRequestedEvent>()
                    .Publish(new BusinessObject.Model.MenuModel
                    {
                        Module = "Dashboard",
                        PresenterClass = "DashboardViewModel"
                    });

我的应用程序中的大多数模块都是此事件的订阅者。他们使用过滤器仅订阅与模块相关的事件

this.eventAggregator.GetEvent<ViewRequestedEvent>()
            .Subscribe(LoadViewRequest, ThreadOption.UIThread, true, i => i.Module == "Dashboard");

视图请求的处理方式如下:

private void LoadRequestedView(MenuModel menuItem)
{
    try
    {
        IDashboardViewModel viewModel = this.container.Resolve(Type.GetType(menuItem.PresenterClass)) as IDashboardViewModel;
        this.regionManager.Regions["ViewRegion"].Add(viewModel.View);
        this.regionManager.Regions["ViewRegion"].Activate(viewModel.View);
        this.eventAggregator.GetEvent<ViewNotificationEvent>().Publish(menuItem.Description);
    }
    catch (ResolutionFailedException) { }
}

您如何评价此实现?如果您认为它很糟糕,请帮助我改进它或提出更好的替代方案。

I've implemented navigation through my application using a Menu control which publish an event using EventAggregator on click of menu item. Something like as shown below,

this.eventAggregator.GetEvent<ViewRequestedEvent>()
                    .Publish(new BusinessObject.Model.MenuModel
                    {
                        Module = "Dashboard",
                        PresenterClass = "DashboardViewModel"
                    });

Most of the Modules in my application are subscriber to this event. They use a filter to subscribe only those events relevant to the Module

this.eventAggregator.GetEvent<ViewRequestedEvent>()
            .Subscribe(LoadViewRequest, ThreadOption.UIThread, true, i => i.Module == "Dashboard");

Request for View is handled like this:

private void LoadRequestedView(MenuModel menuItem)
{
    try
    {
        IDashboardViewModel viewModel = this.container.Resolve(Type.GetType(menuItem.PresenterClass)) as IDashboardViewModel;
        this.regionManager.Regions["ViewRegion"].Add(viewModel.View);
        this.regionManager.Regions["ViewRegion"].Activate(viewModel.View);
        this.eventAggregator.GetEvent<ViewNotificationEvent>().Publish(menuItem.Description);
    }
    catch (ResolutionFailedException) { }
}

How do you rate this implementation? If you think it sucks, help me to improve it or suggest a better alternative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒儿 2024-08-17 06:44:56

我认为这个模型缺乏关注点分离。我通常尝试向 Shell 展示内容。请参阅此问题了解我如何实现这一点。它简化了事情并将 RegionManager 的概念从模块中抽象出来:

将模块与 Prism 又名 CompositeWpf 中的应用程序集成

I think you have a certain lack for separation of concerns with this model. I generally try to keep presentation to the Shell. See this question for how I wold implement this. It simplifies things and abstracts the idea of a RegionManager away from the Modules:

Integrating Modules with Application in Prism aka CompositeWpf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文