2014-10-10 2 views
0

mvvm 라이트 프레임 워크를 배우려면이 http://msdn.microsoft.com/en-us/magazine/jj651572.aspx을 읽는 중입니다. 소스 코드 Friend.cs를 다운로드합니다.Set()와 RaisePropertyChanged()의 차이점

제 질문은 다른 속성의 일부 설정 방법이 다르게 구현되는 이유입니다.

예를 들어 이름에 대한 설정자는 _firstName에 'ref'키워드가 필요한 이유입니다.

Set(FirstNamePropertyName, ref _firstName, value); 

그리고 DateOfBirthString 용 세터

namespace MyFriends.Model 
{ 
    [SimpleSerialize] 
    public class Friend : ObservableObject 
    { 
     /// <summary> 
     /// The <see cref="FirstName" /> property's name. 
     /// </summary> 
     public const string FirstNamePropertyName = "FirstName"; 

     private string _firstName; 

     /// <summary> 
     /// Sets and gets the FirstName property. 
     /// Changes to that property's value raise the PropertyChanged event. 
     /// </summary> 
     [SimpleSerialize(FieldName = "first_name")] 
     public string FirstName 
     { 
      get 
      { 
       return _firstName; 
      } 
      set 
      { 
       Set(FirstNamePropertyName, ref _firstName, value); 
      } 
     } 

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

     private string _lastName; 

     /// <summary> 
     /// Sets and gets the LastName property. 
     /// Changes to that property's value raise the PropertyChanged event. 
     /// </summary> 
     [SimpleSerialize(FieldName = "last_name")] 
     public string LastName 
     { 
      get 
      { 
       return _lastName; 
      } 
      set 
      { 
       Set(LastNamePropertyName, ref _lastName, value); 
      } 
     } 

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

     private string _dateOfBirthString; 

     /// <summary> 
     /// Sets and gets the DateOfBirth property. 
     /// Changes to that property's value raise the PropertyChanged event. 
     /// </summary> 
     [SimpleSerialize(FieldName = "birthday")] 
     public string DateOfBirthString 
     { 
      get 
      { 
       return _dateOfBirthString; 
      } 
      set 
      { 
       _dateOfBirthString = value; 
       RaisePropertyChanged(() => DateOfBirth); 
      } 
     } 

     public DateTime DateOfBirth 
     { 
      get 
      { 
       if (string.IsNullOrEmpty(_dateOfBirthString)) 
       { 
        return DateTime.MinValue; 
       } 

       return DateTime.ParseExact(DateOfBirthString, "d", CultureInfo.InvariantCulture); 
      } 
      set 
      { 
       _dateOfBirthString = value.ToString("d", CultureInfo.InvariantCulture); 
      } 
     } 

     private string _imageUrl; 

     [SimpleSerialize(FieldName = "picture")] 
     public string ImageUrl 
     { 
      get 
      { 
       return _imageUrl; 
      } 
      set 
      { 
       _imageUrl = value; 
       RaisePropertyChanged(() => ImageUri); 
      } 
     } 

     public Uri ImageUri 
     { 
      get 
      { 
       return new Uri(_imageUrl); 
      } 
     } 
    } 
} 

답변

2

Set 방법은 대체이 두 방법의 차이가 있음을

RaisePropertyChanged(() => DateOfBirth); 

때 LINQ 식 평가 될 것인가? "이다 _firstName 필드의 이전 값으로 변경 한 다음 RaisePropertyChanged은만 발생시키고 PropertyChanged 이벤트는 발생시킵니다.이벤트.

는 일반적으로 하나의 방법으로 재산의 세터 내에서 수행해야 할 모든 포장에 의해 속성 선언을 단축하는 데 도움이 있기 때문에 당신은 대부분의 경우에 Set 방법을 사용하는 것이 좋습니다 :

  1. 그것은 업데이트를 전달 된 필드의 값을 value의 내용으로 덮어 쓰고
  2. PropertyChanged 이벤트를 발생시켜이 업데이트에 대한보기를 알립니다.

필드 참조에 의해 전달 될 필요가있는 이유는, (이에 ref _firstName 사용)하지 필드의 내용이 Set 방법에서 필요하지만 필드 자체가 실제로 업데이트된다는 점이다.

RaisePropertyChanged 메서드는 한 속성의 업데이트가 추가 속성에도 영향을 줄 때 유용합니다. 이 경우 이러한 속성의 내용을 수동으로 업데이트해야하며 RaisePropertyChanged 메서드를 호출하여 Views가 실제로 변경된 속성을 알릴 수 있습니다.