Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不动产';无法更改NotifyProperty_C#_Binding_Inotifypropertychanged - Fatal编程技术网

C# 不动产';无法更改NotifyProperty

C# 不动产';无法更改NotifyProperty,c#,binding,inotifypropertychanged,C#,Binding,Inotifypropertychanged,我有一个有3个文件的小应用程序。第一个文件是Authentication,它继承自第二个文件observateObject。此文件继承了INotifyPropertyChanged class Authentication : ObservableObject { public void Start() { Auth = Visibility.Visible; Tab = Visibility.Collapsed; } publi

我有一个有3个文件的小应用程序。第一个文件是
Authentication
,它继承自第二个文件
observateObject
。此文件继承了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()被正确执行,并且设置了正确的
可见性
。当我按下绑定到
命令的
按钮时,
auth.SetView()已执行,但
可见性
未更新

我的结论是,当我加载应用程序时,
可见性设置正确,但一旦加载,它就不会从
Authentication
类更新到
MainWindowViewModel


编辑:下面是对这个问题可能很重要的
observegeObject

public class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

您可以通过身份验证类更新Auth的visisbility,这不会导致触发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事件(在这种情况下,这应该足够了)


您是否通过调试代码验证了您的结论?@DanielKelley,是的。我检查了起作用的每个变量和属性。当我调用
auth.Start()时
public main windowview model
可见性设置为正确。当我从
登录命令
调用auth.SetView()时(或者定期单击按钮,这无关紧要),
Authentication
类中的2个属性被设置,但在
MainWindowViewModel
类中没有设置,该类使用
Authentication
@DanielKelley中的值,我只是好奇。在
MainWindowViewModel
中只做可视化的工作,而在
Authentication
中做逻辑性的工作,是否会“更聪明”?然后使用
Authentication
中的事件(任何其他选项)直接设置
MainWindowViewModel
中的属性?我知道,如果在应用程序运行时在
MainWindowViewModel
中设置属性,它会起作用。我将
observeObject
代码添加到原始帖子中。你的建议是正确的,但在这种情况下不是问题。是的,是的!您基本上没有为它提供属性名称(因此默认值
propertyName=”“
),这使得它根本无法工作。将
Auth
的实现更改为我建议的,它应该可以正常工作。在我开始争论之前,您最好检查我的
INotifyPropertyChanged
代码。它使用
System.Runtime.CompilerServices
中的
[CallerMemberName]
。这将获取属性的名称并填充它,而无需在调用该方法时输入属性的名称
CallerMemberName
它的描述如下:
允许您获取方法调用方的方法或属性名。
My bad,我完全忽略了这一点。但是,嘿,我现在可能发现了问题,检查我的最新答案。如果我错了,请纠正我。:)Tnx的答复。我明天会检查一下,告诉你进展如何。
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();
        }
    }
}