从 Prism 中的导航控件动态加载视图的最佳方法是什么
我已经使用菜单控件在我的应用程序中实现了导航,该菜单控件在单击菜单项时使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这个模型缺乏关注点分离。我通常尝试向 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