Silverlight 4 和 Prism 中的弹出窗口

发布于 2024-12-01 02:07:51 字数 77 浏览 0 评论 0原文

在 Silverlight 和 PRISM 中,通过从不同模块中的 ViewModel 传递参数来打开一个模块中的弹出子窗口的好方法是什么。

In Silverlight and PRISM, what is the good way to open a popup child window which is in one Module by passing a parameter from a ViewModel in different Module.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-12-08 02:07:51

创建两个模块都知道的公共接口/类,称为 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{}

模块 A 中的 ViewModel 引发该事件。
模块 B 中的控制器注册事件并对事件做出反应。
模块B中的控制器将子窗口服务作为依赖项。
当事件发生时,控制器将在模块 B 中创建 VM 并将参数传递给它,从事件中,它还将使用服务来显示子窗口。

Create a common interface/class known to both module, called IChildWindowService, register the IChildWindowServe/ChildWindowService in the bootstrapper.

//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();
    }
}

Create a common CompositePresentationEvent, this event will pass the parameters from point a to point b

public class OpenChildWindowWithParameters : CompositePresentationEvent<ParamEventArgs>{}

The ViewModel in Module A raises the Event.
The Controller in Module B registers and reacts to the Event.
The Controller in Module B takes the child window service as a dependency.
When the event is raised the Controller will create the VM in Module B and pass the parameters to it, from the event, it will also use the Service to display a ChildWindow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文