【发布时间】:2018-01-15 13:06:43
【问题描述】:
编辑:请参阅类结构。 INotifyPropertyChanged 在 ComMethodA 情况下工作正常并按预期触发事件。为什么它在 ComMethodB 的情况下不起作用?
编辑 2: 请参考视图模型代码。
在我的应用程序中,我有几个视图和视图模型。 所有的 ViewModel 都继承自 ViewModelBase,它创建了我的模型的单例对象,我们将其称为 MainModel,结构如下:
public class MainModel : INotifyPropertyChanged
{
public string Name {get; set; }
public List<ISystem> SystemsList{get;set;}
public IComMethod ComMethodA {get;set;} // TEST 1
public MainModel()
{
ComMethodA = new ClassC();
// let's assume that the other props are initialized
}
}
public class System: ISystem, INotifyPropertyChanged
{
public IComMethod ComMethodB {get;set;} // TEST 2
public System()
{
ComMethodB = new classC(); // classC implements INotifyPropertyChanged, IComMethod
}
}
public class ClassC() : IComMethod, INotifyPropertyChanged
{
public bool isOpen {get;set;}
public ClassC()
{
// ctor..
}
}
ViewModelA:
ctor()
{
MainModel.SystemsList[0].ComMethodB.PropertyChanged += ComMethodB_PropertyChanged;
MainModel.ComMethodA.PropertyChanged += ComMethodA_PropertyChanged;
}
private void ComMethodB_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
MessageBox.Show("TextB");
}
private void ComMethodA_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
MessageBox.Show("TextA");
}
视图模型 B:
MainModel.ComMethodA.isOpen = true; // FIRES event in ViewModelA
MainModel.SystemsList[0].ComMethodB.isOpen = true; // DOESN'T fires event in ViewModelA
在 ViewModelA 中,我正在为 TEST 1 和 TEST 2 属性实现属性更改事件。 在 ViewModelB 中,我正在更改“isOpen”属性。
问题是该事件仅在“ComMethodA”场景中触发,但我希望它在“ComMethodB”场景中触发。
谢谢
【问题讨论】:
-
你必须提出这个事件。在提供的代码中没有任何地方表明正在引发事件。建议您创建一个具有通用功能的基础视图模型并从中派生其他类
-
@gr1d3r
INotifyPropertyChanged只是一个接口。它没有实际功能 -
正如FCin所说,实现
INotifyPropertyChanged只是意味着你的类必须有具有适当参数和返回类型的方法,你仍然必须实现它们并调用它们。 -
没有看到实现,我们很难/不可能帮助您找出问题所在。另外,你怎么知道事件没有被触发?
-
事件在哪里引发?仅更改
isOpen不会自动引发PropertyChanged事件。如果您的ViewModelBase实现确实解决了这个问题,它是如何做到的?