C# 解释数据绑定windows phone应用程序的ItemViewModel.cs中的setter和NotifyPropertyChange方法 private string\u lineOne; /// ///示例ViewModel属性;此属性在视图中用于使用绑定显示其值。 /// /// 公共字符串第一行 { 得到 { 返回_lineOne; } 设置 { 如果(值!=\u lineOne) { _lineOne=值; NotifyPropertyChanged(“LineOne”); } } } 私有字符串_lineTwo; /// ///示例ViewModel属性;此属性在视图中用于使用绑定显示其值。 /// /// 公共字符串第二行 { 得到 { 返回第二行; } 设置 { 如果(值!=\u第二行) { _第二行=值; NotifyPropertyChanged(“第二行”); } } } 私有字符串_第三行; /// ///示例ViewModel属性;此属性在视图中用于使用绑定显示其值。 /// /// 公共字符串第三行 { 得到 { 返回第三行; } 设置 { 如果(值!=\u第三行) { _第三行=值; NotifyPropertyChanged(“第三行”); } } } 公共事件属性更改事件处理程序属性更改; 私有void NotifyPropertyChanged(字符串propertyName) { PropertyChangedEventHandler处理程序=PropertyChanged; if(null!=处理程序) { 处理程序(这是新的PropertyChangedEventArgs(propertyName)); } } }

C# 解释数据绑定windows phone应用程序的ItemViewModel.cs中的setter和NotifyPropertyChange方法 private string\u lineOne; /// ///示例ViewModel属性;此属性在视图中用于使用绑定显示其值。 /// /// 公共字符串第一行 { 得到 { 返回_lineOne; } 设置 { 如果(值!=\u lineOne) { _lineOne=值; NotifyPropertyChanged(“LineOne”); } } } 私有字符串_lineTwo; /// ///示例ViewModel属性;此属性在视图中用于使用绑定显示其值。 /// /// 公共字符串第二行 { 得到 { 返回第二行; } 设置 { 如果(值!=\u第二行) { _第二行=值; NotifyPropertyChanged(“第二行”); } } } 私有字符串_第三行; /// ///示例ViewModel属性;此属性在视图中用于使用绑定显示其值。 /// /// 公共字符串第三行 { 得到 { 返回第三行; } 设置 { 如果(值!=\u第三行) { _第三行=值; NotifyPropertyChanged(“第三行”); } } } 公共事件属性更改事件处理程序属性更改; 私有void NotifyPropertyChanged(字符串propertyName) { PropertyChangedEventHandler处理程序=PropertyChanged; if(null!=处理程序) { 处理程序(这是新的PropertyChangedEventArgs(propertyName)); } } },c#,visual-studio,windows-phone-8,C#,Visual Studio,Windows Phone 8,} 这是来自VS windows phone的数据绑定应用程序的典型ItemViewModel.cs页面 我有两个Q: 首先,在所有属性的setter中,“if”语句的功能是什么。 其次,请解释NotifyPropertyChanged方法(逐行)以及事件PropertyChanged的工作原理 我有两个问题:第一,“if”语句在 所有属性的setter 它确保只有在属性值实际更改时才会引发PropertyChanged事件。当它被设置为它已经具有的值时,只会跳过更新,因为它不会更改任何内容 第

}

这是来自VS windows phone的数据绑定应用程序的典型ItemViewModel.cs页面

我有两个Q:

首先,在所有属性的setter中,“if”语句的功能是什么。 其次,请解释NotifyPropertyChanged方法(逐行)以及事件PropertyChanged的工作原理

我有两个问题:第一,“if”语句在 所有属性的setter

它确保只有在属性值实际更改时才会引发
PropertyChanged
事件。当它被设置为它已经具有的值时,只会跳过更新,因为它不会更改任何内容

第二,请向我解释
NotifyPropertyChanged
方法(逐行)以及事件
PropertyChanged


这是举办活动的标准方式。检查。它描述了事件以及如何处理它们。快捷方式:
null
需要检查以防止
NullReferenceException
。要知道为什么要使用局部变量,请阅读Eric Lippert的这篇文章:。

这意味着,如果我有一个应用程序不需要从web上不断更新,我可以完全删除setters属性和NotifyPropertyChanged,而不必压缩我的应用程序的功能。不,你不应该这样做。如果没有
INotifyPropertyChanged
,则当您更改ViewModel中的值时,UI将不会更新。
    private string _lineOne;
    /// <summary>
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
    /// </summary>
    /// <returns></returns>
    public string LineOne
    {
        get
        {
            return _lineOne;
        }
        set
        {
            if (value != _lineOne)
            {
                _lineOne = value;
                NotifyPropertyChanged("LineOne");
            }
        }
    }

    private string _lineTwo;
    /// <summary>
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
    /// </summary>
    /// <returns></returns>
    public string LineTwo
    {
        get
        {
            return _lineTwo;
        }
        set
        {
            if (value != _lineTwo)
            {
                _lineTwo = value;
                NotifyPropertyChanged("LineTwo");
            }
        }
    }

    private string _lineThree;
    /// <summary>
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
    /// </summary>
    /// <returns></returns>
    public string LineThree
    {
        get
        {
            return _lineThree;
        }
        set
        {
            if (value != _lineThree)
            {
                _lineThree = value;
                NotifyPropertyChanged("LineThree");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}