为什么没有人使用INotifyPropertyChanging? [英] Why doesn't anyone use the INotifyPropertyChanging?

查看:510
本文介绍了为什么没有人使用INotifyPropertyChanging?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道MVVM大量使用INotifyPropertyChanged的,但我从来没有见过的INotifyPropertyChanging的任何使用。什么理由?

I know MVVM heavily uses the INotifyPropertyChanged, but I have never seen any usage of the INotifyPropertyChanging. Any reason why?

如果我想用这个,这将是该融入我的MVVM框架的好办法?我知道你不应该在您的视图模型使用的MessageBox ,因为你不能单元测试。所以,一个人如何会去扔了一个警告,然后使用PropertyChange继续就是否适用?

If I did want to use this, what would be a good way to integrate this into my MVVM Framework? I know you're not supposed to use MessageBox on your ViewModel because then you can't unit test it. So how would one go about throwing up an alert, then continuing on with the PropertyChange if applicable?

推荐答案

东西要牢记关于 INotifyPropertyChanging 是你不能的阻止发生的变化。这只是使您能够记录所发生的变化。

Something to keep in mind about INotifyPropertyChanging is you can't stop the change from happening. This merely allows you to record that the change occurred.

我用它在矿井改变跟踪的框架,但它不是以停止改变适当的方法

I use it in a framework of mine for change tracking, but it isn't an appropriate method for halting changes.

您可以扩展你的 ViewModelBase 使用自定义接口/事件对:

You could extend your ViewModelBase with a custom interface/event pair:

delegate void AcceptPendingChangeHandler(
    object sender,
    AcceptPendingChangeEventArgs e);

interface IAcceptPendingChange
{
    AcceptPendingChangeHandler PendingChange;
}

class AcceptPendingChangeEventArgs : EventArgs
{
    public string PropertyName { get; private set; }
    public object NewValue { get; private set; }
    public bool CancelPendingChange { get; set; }
    // flesh this puppy out
}

class ViewModelBase : IAcceptPendingChange, ...
{
    protected virtual bool RaiseAcceptPendingChange(
        string propertyName,
        object newValue)
    {
        var e = new AcceptPendingChangeEventArgs(propertyName, newValue)
        var handler = this.PendingChange;
        if (null != handler)
        {
            handler(this, e);
        }

        return !e.CancelPendingChange;
    }
}



在这一点上你需要通过将其添加公约视图模型:

At this point you'd need to add it by convention to your view models:

class SomeViewModel : ViewModelBase
{
     public string Foo
     {
         get { return this.foo; }
         set
         {
             if (this.RaiseAcceptPendingChange("Foo", value))
             {
                 this.RaiseNotifyPropertyChanging("Foo");
                 this.foo = value;
                 this.RaiseNotifyPropretyChanged("Foo");
             }
         }
     }
}

这篇关于为什么没有人使用INotifyPropertyChanging?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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