问题
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