2014-11-03 25 views
0

我使用mvvmlight,我觉得默认的实现(从mvvminpcmsg的代码片段)的INPC的是:为什么mvvmlight默认实现中的公共常量字符串属性?

/// <summary> 
    /// The <see cref="MyProperty" /> property's name. 
    /// </summary> 
    public const string MyPropertyPropertyName = "MyProperty"; 

    private bool _myProperty = false; 

    /// <summary> 
    /// Sets and gets the MyProperty property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// This property's value is broadcasted by the MessengerInstance when it changes. 
    /// </summary> 
    public bool MyProperty 
    { 
     get 
     { 
      return _myProperty; 
     } 

     set 
     { 
      if (_myProperty == value) 
      { 
       return; 
      } 

      RaisePropertyChanging(MyPropertyPropertyName); 
      var oldValue = _myProperty; 
      _myProperty = value; 
      RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true); 
     } 
    } 

我不知道为什么添加了公共常量字符串?

public const string MyPropertyPropertyName = "MyProperty"; 

我不认为它是inpc实现所必需的,我也没有看到它的任何用法。 那么为什么加入?

回答

0

所有INPC实现都需要某种方式来解决哪些属性引发更改通知。

  • 传入财产
  • 的字符串名称用一个表达式解析属性名
  • 使用CallerMemberNameAttribute属性:这通常是由完成。

在您的示例中,它使用属性的字符串名称。

+0

但为什么它是'公共'? – Felix 2014-11-04 09:10:41

+0

@费:不知道那个。 – 2014-11-04 16:01:50

1

该属性的公共常量字符串被添加用于您不在类中但需要处理属性更改事件的情况。

相关问题