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));
}
}
}
코드를 디버깅하여 결론을 확인 했습니까? –
@DanielKelley, 예. 각 변수와 속성을 확인했습니다. 'public MainWindowViewModel'에서'auth.Start();'를 호출하면 Visibilities가 올바르게 설정됩니다. 'LogIn Command' (또는 일반 버튼 클릭과 상관없이)에서 auth.SetView()를 호출하면'Authentication' 클래스의 2 개 속성이 설정되지만 값을 사용하는'MainWindowViewModel' 클래스에는 설정되지 않습니다 인증에서. – Krowi
@DanielKelley, 궁금합니다. 'MainWindowViewModel'과'Authentication'에서 논리적 인 것들을 시각적으로 처리하는 것이 "더 똑똑"할 수 있습니까? 그런 다음'Authentication '의 이벤트 (다른 옵션)를 사용하여'MainWindowViewModel'의 속성을 직접 설정합니까? 응용 프로그램이 실행되는 동안'MainWindowViewModel'에 속성을 설정하면 작동하는 것을 알 수 있습니다. – Krowi