在Silverlight和PRISM中,通过从不同模块中的ViewModel传递参数,在一个模块中打开弹出子窗口的好方法是什么。
答案 0 :(得分: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中的控制器注册并响应事件。 模块B中的Controller将子窗口服务作为依赖项。 当事件被引发时,Controller将在模块B中创建VM并将参数传递给它,从事件中,它还将使用服务来显示ChildWindow。