2014-09-08 2 views
1

3 파일이있는 작은 응용 프로그램이 있습니다. 첫 번째 파일은 두 번째 파일 ObservableObject에서 상속받은 Authentication입니다. 이 파일은 INotifyPropertyChanged을 상속합니다.속성 NotifyPropertyChanged 얻을 수 없습니다

class Authentication : ObservableObject 
{ 
    public void Start() 
    { 
     Auth = Visibility.Visible; 
     Tab = Visibility.Collapsed; 
    } 

    public void SetView() 
    { 
     Auth = Visibility.Collapsed; 
     Tab = Visibility.Visible; 
    } 

    public Visibility Auth { get; set; } 
    public Visibility Tab { get; set; } 
    public Visibility Admin { get; set; } 
    public Visibility Planner { get; set; } 
    public Visibility WorkPrep { get; set; } 
    public Visibility Leader { get; set; } 
    public Visibility PreSet { get; set; } 
    public Visibility Measure { get; set; } 
    public Visibility Worker { get; set; } 
} 

제 3 파일은 내 View의 ViewModel입니다. 나는 응용 프로그램을 시작할 때 지금

class MainWindowViewModel : ObservableObject 
{ 
    private Authentication auth = new Authentication(); 

    public MainWindowViewModel() 
    { 
     LogIn = new RelayCommand(() => auth.SetView(),() => (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) ? false : true); 
     auth.Start(); 
    } 

    public ICommand LogIn { get; set; } 
    public Visibility Auth 
    { 
     get 
     { 
      return auth.Auth; 
     } 
     set 
     { 
      auth.Auth = value; 
      NotifyPropertyChanged(); 
     } 
    } 
    public Visibility Tab 
    { 
     get 
     { 
      return auth.Tab; 
     } 
     set 
     { 
      auth.Tab = value; 
      NotifyPropertyChanged(); 
     } 
    } 
} 

, auth.Start(); 바로 실행하고 올바른 Visibility가 설정됩니다. LogIn에 바인딩 된 Button을 누르면 auth.SetView();이 실행되지만 Visibilities은 업데이트되지 않습니다.

내 결론은 응용 프로그램을로드 할 때 Visibilities이 올바르게 설정되었지만 일단로드되면 Authentication 클래스에서 MainWindowViewModel 클래스로 업데이트되지 않습니다.


편집 : 여기이 질문에 중요 할 수있는 ObservableObject 클래스입니다.

public class ObservableObject : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
+0

코드를 디버깅하여 결론을 확인 했습니까? –

+0

@DanielKelley, 예. 각 변수와 속성을 확인했습니다. 'public MainWindowViewModel'에서'auth.Start();'를 호출하면 Visibilities가 올바르게 설정됩니다. 'LogIn Command' (또는 일반 버튼 클릭과 상관없이)에서 auth.SetView()를 호출하면'Authentication' 클래스의 2 개 속성이 설정되지만 값을 사용하는'MainWindowViewModel' 클래스에는 설정되지 않습니다 인증에서. – Krowi

+0

@DanielKelley, 궁금합니다. 'MainWindowViewModel'과'Authentication'에서 논리적 인 것들을 시각적으로 처리하는 것이 "더 똑똑"할 수 있습니까? 그런 다음'Authentication '의 이벤트 (다른 ​​옵션)를 사용하여'MainWindowViewModel'의 속성을 직접 설정합니까? 응용 프로그램이 실행되는 동안'MainWindowViewModel'에 속성을 설정하면 작동하는 것을 알 수 있습니다. – Krowi

답변

0

인증 클래스를 통해 인증 클래스를 업데이트하면 NotifyProperty 이벤트가 발생하지 않습니다. 나는 인증 클래스 내부 NotifyPropertyChanged 메소드의 호출을 추가 :

class Authentication : ObservableObject 
{ 
    public void Start() 
    { 
     Auth = Visibility.Visible; 
     Tab = Visibility.Collapsed; 
    } 

    public void SetView() 
    { 
     Auth = Visibility.Collapsed; 
     Tab = Visibility.Visible; 
    } 

    private Visibility auth; 
    public Visibility Auth 
    { 
     get 
     { 
      return auth; 
     } 
     set 
     { 
      auth= value; 
      NotifyPropertyChanged(); 
     } 
    } 

    private Visibility tab; 
    public Visibility Tab 
    { 
     get 
     { 
      return tab; 
     } 
     set 
     { 
      tab = value; 
      NotifyPropertyChanged(); 
     } 
    } 
    // etc 
} 

지금, 우리가 MainWindowViewModel의 모든 NotifyProperty 이벤트 (이이 경우에 충분합니다)를 에코.

class MainWindowViewModel : ObservableObject 
{ 
    private Authentication auth = new Authentication(); 

    public MainWindowViewModel() 
    { 
     LogIn = new RelayCommand(() => auth.SetView(),() => (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)) ? false : true); 

     // Echo the PropertyChanged events from our auth class 
     auth.PropertyChanged += (sender, e) => 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(sender, e); 
     } 

     auth.Start(); 
    } 

    public ICommand LogIn { get; set; } 
    public Visibility Auth 
    { 
     get 
     { 
      return auth.Auth; 
     } 
     set 
     { 
      auth.Auth = value; 
      NotifyPropertyChanged(); 
     } 
    } 
    public Visibility Tab 
    { 
     get 
     { 
      return auth.Tab; 
     } 
     set 
     { 
      auth.Tab = value; 
      NotifyPropertyChanged(); 
     } 
    } 
} 
+0

원본 'ObservableObject' 코드를 원래 게시물에 추가했습니다. 귀하의 제안은 정확하지만이 경우에는 문제가되지 않습니다. – Krowi

+0

예, 그렇습니다! 기본적으로 속성 이름을 제공하지 않으므로 (기본값은'propertyName = ""'기본값), 전혀 작동하지 않게됩니다. Auth의 구현을 내가 제안한대로 변경하면 제대로 작동 할 것입니다. –

+0

필자가 논하기 시작하기 전에,'INotifyPropertyChanged' 코드를 확인하는 것이 좋습니다. 그것은'System.Runtime.CompilerServices'에서'[CallerMemberName]'을 사용합니다. 이것은 속성의 이름을 가져오고 그 메서드를 호출 할 때 속성 이름을 넣지 않고 채 웁니다. 'CallerMemberName'에 대한 설명은 다음과 같습니다 :'메서드 호출자의 메서드 또는 속성 이름을 얻을 수 있습니다 .' – Krowi

관련 문제