0

我使用 NotifyPropertyWeaverMsBuildTask 来处理自动属性的 NotifyPropertyChanged。我知道属性值更改时 OnPropertyChanged() 方法会上升。但是当这个方法被调用时,属性的值被改变并且旧的值丢失了。有没有办法获得旧值?

坦克。

4

2 回答 2

3

INotifyPropertyChanged不提供获取属性先前值的方法;你必须实现你自己的。

于 2012-08-06T11:30:38.970 回答
3

如果你想在里面使用旧值OnPropertyChanged然后这样写

public void OnPropertyChanged(string propertyName, object before, object after)

那么如果你的代码看起来像这样

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Name { get; set; }

    public void OnPropertyChanged(string propertyName, object before, object after)
    {
        // do something with before/after
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这将被注入

public class Person : INotifyPropertyChanged
{
    private string name;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Name
    {
        get { return name; }
        set
        {
            object before = Name;
            name = value;
            OnPropertyChanged("Name", before, Name);
        }
    }

    public void OnPropertyChanged(string propertyName, object before, object after)
    {            
        // do something with before/after
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

此处提供更多信息https://github.com/SimonCropp/NotifyPropertyWeaver/wiki/BeforeAfter

这符合您的要求吗?

于 2012-08-07T13:41:07.627 回答