C# 不动产';无法更改NotifyProperty
我有一个有3个文件的小应用程序。第一个文件是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
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();
}
}
}