在 Silverlight 和 PRISM 中,通过从不同模块中的 ViewModel 传递参数来打开位于一个模块中的弹出子窗口的好方法是什么。
问问题
1409 次
1 回答
1
创建两个模块都知道的通用接口/类,称为 IChildWindowService,在引导程序中注册 IChildWindowServe/ChildWindowService。
//Highly simplified version
//Can be improved by window reuse, parameter options, stronger eventing
public class ChildWindowService : IChildWindowService
{
public ChildWindowService(IServiceLocator container)
{
_container = container;
}
public void Show<TViewModel>(TViewModel viewModel = null, Action<TViewModel, bool?> callBack = null) where TViewModel is IViewModel
{
var viewName = typeof(TViewModel).Name.Replace("Model", string.Empty);
// In bootstrapper register all instances of IView or register each view one by one
var view = _container.GetInstance<IView>(viewName);
viewModel = viewModel ?? _container.GetInstance<TViewModel>();
view.DataContext = viewModel;
var window = new ChildWindow();
window.Content = view;
var handler = (s,e) => { window.Close(); }
viewModel.RequestClose += handler;
view.Closed += (s,e) => { viewModel.RequestClose -= handler; }
// In silverlight all windows show as Modal, if you are using a third party you can make a decision here
window.Show();
}
}
创建一个普通的CompositePresentationEvent,这个事件会将参数从a点传到b点
public class OpenChildWindowWithParameters : CompositePresentationEvent<ParamEventArgs>{}
模块 A 中的 ViewModel 引发了事件。模块 B 中的控制器注册并响应事件。Module B 中的 Controller 将子窗口服务作为依赖。引发事件时,控制器将在模块 B 中创建 VM 并将参数传递给它,从事件中,它还将使用服务来显示 ChildWindow。
于 2011-08-25T11:04:09.137 回答