3

내 엔티티에 LINQ-to-SQL을 사용하는 Windows Phone 8 응용 프로그램을 작성하고 있습니다.LINQ-to-SQL을 사용하여 INotifyPropertyChanged 구현

내 새로운 방법에 대한 속성 setter을 변경하려는
[Table] 
public class Item : INotifyPropertyChanged, INotifyPropertyChanging 
{ 
    private int _itemId; 

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)] 
    public int ItemId 
    { 
     get { return _itemId; } 
     set 
     { 
      if (_itemId != value) 
      { 
       NotifyPropertyChanging("ItemId"); 
       _itemId = value; 
       NotifyPropertyChanged("ItemId"); 
      } 
     } 
    } 

    [Column] 
    internal int? _groupId; 

    private EntityRef<Board> _group; 

    [Association(Storage = "_group", ThisKey = "_groupId", OtherKey = "GroupId", IsForeignKey = true)] 
    public Group Group 
    { 
     get { return _group.Entity; } 
     set 
     { 
      NotifyPropertyChanging("Group"); 
      _group.Entity = value; 

      if (value != null) 
      { 
       _groupId = value.BoardId; 
      } 

      NotifyPropertyChanging("Group"); 
     } 
    } 
} 

, 내가 여기 설립 한 : 나의 현재 구현은 간단 INotifyPropertyChanging/INotififyPropertyChanged 방법을 사용 http://danrigby.com/2012/04/01/inotifypropertychanged-the-net-4-5-way-revisited/

protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null) 
{ 
    if (object.Equals(storage, value)) return false; 

    this.OnPropertyChanging(propertyName); 
    storage = value; 
    this.OnPropertyChanged(propertyName); 
    return true; 
} 

그것은을 위해 구현하기 쉽다 속성은 과 같습니다. 항목 ID는입니다. 그러나 그룹 ID는 값이 _group.Entity에 설정되어야하고 이는 참조로 전달 될 수 없기 때문에 그룹 ID를 구현하는 방법을 모르겠습니다. nce.

이 내 해결 (아직 테스트하지)입니다,하지만 난 PropertyChanging /하여 PropertyChanged 이벤트가 조기에 발생합니다 생각 :이 문제에 대한 명확한 해결책은

public Group Group 
    { 
     get { return _group.Entity; } 
     set 
     { 
      var group = _group.Entity; 

      if (SetProperty(ref group, value)) 
      { 
       _group.Entity = group; 

       if (value != null) 
       { 
        _groupId = value.GroupId; 
       } 
      } 
     } 
    } 

있습니까?

답변

2

해결책을 찾았습니다. 이 같은

protected T SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null) 
    { 
     if (EqualityComparer<T>.Default.Equals(field, value)) 
     { 
      return value; 
     } 

     NotifyPropertyChanging(propertyName); 
     field = value; 
     NotifyPropertyChanged(propertyName); 

     return value; 
    } 

    protected T SetProperty<T>(ref EntityRef<T> field, T value, [CallerMemberName] string propertyName = null) where T : class 
    { 
     NotifyPropertyChanging(propertyName); 
     field.Entity = value; 
     NotifyPropertyChanged(propertyName); 

     return value; 
    } 

전화를 :

public Group Group 
    { 
     get { return _board.Entity; } 
     set 
     { 
      if (SetProperty(ref _group, value) != null) 
      { 
       _groupId = value.GroupId; 
      } 
     } 
    } 
그냥 다른 구현 방법을 과부하
관련 문제