2014-11-03 56 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

該屬性的公共常量字符串被添加用於您不在類中但需要處理屬性更改事件的情況。

相關問題