What is the best method to load Views dynamically from a Navigation control in Prism

大城市里の小女人 提交于 2019-12-13 16:14:58

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/1678423/what-is-the-best-method-to-load-views-dynamically-from-a-navigation-control-in-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!