WPF MVVM代码背后的最佳实践 [英] WPF MVVM Code Behind Best Practices

查看:106
本文介绍了WPF MVVM代码背后的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个学生的学习C#使用MVVM模式WPF。最近我一直在我的应用程序(自定义闪屏)时,我不希望它不应该被关闭的[艺术。
我一直在寻找这样做没有代码隐藏的好方法网页。不幸的是天后我还没有找到一个令人满意的方式。
后来我才想办法做我自己,只有一个在我看来的构造函数的代码行的帮助。它仍然使我的代码可测试和解耦查看的代码。现在的问题是,有没有做什么,我试图做一个更好的方式:



我的界面我的ViewModel

 公共接口IPreventCloseViewModel 
{
布尔PreventClose {搞定;组; }
}



扩展的查看

 公共静态类PreventCloseViewModelExtension 
{
///<总结>
///在视图的构造函数中使用这个扩展方法。
///< /总结>
///< PARAM NAME =元素>< /参数>
公共静态无效PreventCloseViewModel(此窗口元素)
{
变种的DataContext = element.DataContext为IDisposable的;
如果(DataContext的是IPreventCloseViewModel)
{
element.Closing + =委托(对象发件人,发送CancelEventArgs参数)
{
如果(DataContext的是IPreventCloseViewModel)
{
args.Cancel =(DataContext的作为IPreventCloseViewModel).PreventClose;
}
};
}
}
}



代码隐藏的查看

 公共部分类闪屏
{
公共闪屏()
{
的InitializeComponent();
this.PreventCloseViewModel();
}
}


解决方案

MVVM并不意味着你不能使用代码隐藏。



MVVM意味着你的应用程序逻辑不应该依赖于UI元素。



您可以非常顺利地处理代码事件背后(如 Window.Closing )和发送消息或执行中的视图模型方法,采取相应的措施。



在这里,你没有违反MVVM通过将事件处理程序后面的代码。你会被打破MVVM如果你将确定是否该应用程序可以在代码后面封闭的逻辑。这是应用程序逻辑的责任,而应用逻辑家住的ViewModels,不是视图。


I'm a student learning C# with WPF using the MVVM pattern. Recently I have been working on a [art of my application (a custom splash screen) that should not be closed when I don't want it to. I have been searching the web for a good way of doing this without code-behind. Unfortunately after days I still did not find a satisfying way. Then I came to think of a way to do it myself, with help of just one line of code in the constructor of my view. It still makes my code testable and decouples the code from the View. The question is, is there a better way of doing what I'm trying to do:

My interface for my ViewModel

public interface IPreventCloseViewModel
{
    bool PreventClose { get; set; }
}

The extension for the View

public static class PreventCloseViewModelExtension
{
    /// <summary>
    /// Use this extension method in the constructor of the view.
    /// </summary>
    /// <param name="element"></param>
    public static void PreventCloseViewModel(this Window element)
    {
        var dataContext = element.DataContext as IDisposable;
        if (dataContext is IPreventCloseViewModel)
        {
            element.Closing += delegate(object sender, CancelEventArgs args)
                                   {
                                       if (dataContext is IPreventCloseViewModel)
                                       {
                                           args.Cancel = (dataContext as IPreventCloseViewModel).PreventClose;
                                       }
                                   };
        }
    }
}

The code-behind for the View

public partial class SplashScreen
{
    public SplashScreen()
    {
        InitializeComponent();
        this.PreventCloseViewModel();
    }
}

解决方案

MVVM does not mean that you cannot use Code-Behind.

MVVM means that your application logic should not be tied to UI elements.

You can perfectly well handle events in code behind (such as Window.Closing), and "send messages" or execute methods in the ViewModel to react to that.

Here, you are not breaking MVVM by placing the event handler in code behind. You would be breaking MVVM if you were placing the logic that determines whether the application can be closed in code behind. That is a responsibility of the application logic, and the application logic lives in ViewModels, not Views.

这篇关于WPF MVVM代码背后的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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