如何实现MVVM对话架构 [英] How to implement dialog architecture in MVVM

查看:208
本文介绍了如何实现MVVM对话架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个WPF 4.0 - 基于PRISM框架(统一容器)MVVM应用

I am developing a WPF 4.0 - MVVM application based on PRISM framework (Unity Container).

我想知道什么是落实在MVVM对话的最好方式模式。
我打算使用相当多的在我的应用程序,所以我想要的东西可重用的。

I was wondering what is the best way to implement dialogs in the mvvm pattern. I am planning to use quite a few in my application so I want something reusable.

推荐答案

由于您使用的棱镜/统一实施您的视图模型的调解模式。
1.添加DialogService(IDialogService)模块到项目中。
2.含对话模块注册它们的IDialogService。
不要忘记申报DialogServiceModule作为ModuleDependency。
3的ViewModels现在使用的IDialogService显示所需的对话框。

Since you are using Prism/Unity implement the mediator pattern for your View Models. 1. Add a DialogService (IDialogService) module to your project. 2. Modules containing dialogs register them with the IDialogService. Don't forget to declare DialogServiceModule as a ModuleDependency. 3. ViewModels now use the IDialogService to show the required dialog.

public interface IDialogService
{
    void    RegisterDialog  (string dialogID, Type type);
    bool?   ShowDialog      (string dialogID);
}
public class DialogService : IDialogService
{
    private IUnityContainer       m_unityContainer;
    private DialogServiceRegistry m_dialogServiceRegistry;

    public DialogService(IUnityContainer unityContainer)
    {
        m_unityContainer = unityContainer;
        m_dialogServiceRegistry = new DialogServiceRegistry();
    }
    public void RegisterDialog(string dialogID, Type type)
    {
        m_dialogServiceRegistry.RegisterDialog(dialogID, type);
    }

    public bool? ShowDialog(string dialogID)
    {
        Type type = m_dialogServiceRegistry[dialogID];
        Window window  = m_unityContainer.Resolve(type) as Window;
        bool? dialogResult = window.ShowDialog();

        return dialogResult;
    }



}

}

如果您使用视图模型的事件和放大器;在视图处理程序,使用WeakEventHandler模​​式,以消除潜在的资源泄漏。
也,它有可能为多个视图被连接到相同的视图模型。
我已经在项目上曾经有一个视图模型 - >一个View。而且还1视图模型 - >多个视图。
只是一些使你的设计决策时要考虑的。

If you use ViewModel events & handlers in the View, use the WeakEventHandler pattern to eliminate a potential resource leak. Also, it is possible for multiple Views to be attached to the same ViewModel. I've worked on projects with one ViewModel -> one View. But also one ViewModel -> multiple Views. Just something to consider when making your design decisions.

这篇关于如何实现MVVM对话架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆