属性don';无法获取NotifyPropertyChanged

本文关键字:获取 NotifyPropertyChanged don 属性 | 更新日期: 2023-09-27 18:21:09

我有一个包含3个文件的小应用程序。第一文件是从第二文件CCD_ 2继承的CCD_。此文件继承INotifyPropertyChanged

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; }
}

我的第三个文件是我的视图的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被设置。当我按下绑定到Command LogInButton时,执行auth.SetView();,但不更新Authentication0。

我的结论是,当我加载应用程序时,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));
        }
    }
}

属性don';无法获取NotifyPropertyChanged

通过Authentication类更新Auth的可见性,这不会导致NotifyProperty事件被触发。我在Authentication类中添加了对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();
        }
    }
}