检查 WPF 属性资源库中的值是否实际更改



在 WPF 中,在调用 NotifyPropertyChange 之前,检查模型属性的值是否实际会在属性资源库中更改是否实际更改是一种最佳做法?

例如:

    private string foobar;
    public string Foobar
    {
        get
        {
            return this.foobar;
        }
        set
        {
            if (value != this.foobar)
            {
                this.foobar = value;
                this.NotifyPropertyChanged("Foobar");
            }
        }
    }

另一种方法是不检查,每次都调用 NotifyPropertyChanged:

    private string foobar;
    public string Foobar
    {
        get
        {
            return this.foobar;
        }
        set
        {
            this.foobar = value;
            this.NotifyPropertyChanged("Foobar");
        }
    }

我已经看到代码示例中使用了这两种样式。每种方法的优点和缺点(如果有的话)是什么?

我总是进行检查,因为该事件似乎暗示应该发生实际更改。

(还可以防止不必要的更新)

最新更新