c# - c# 在类成员上使用 INotifyPropertyChanged 继承时出现初始化错误
问题描述
我有一个Main
类,我在其中定义了另一个类的实例:
namespace testRun
{
class Main
{
/// Initialize object to contain various project data
public static CurrentState currentState = new CurrentState();
...
public void Event1(object sender, Args e)
{
ProcessClear();
}
public static void ProcessClear()
{
currentState.quantityDictionary.Clear();
}
}
这个CurrentState
类看起来像这样:
namespace testrun
{
public class CurrentState : INotifyPropertyChanged
{
/// Constructor
public CurrentState() { }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
private Dictionary<string, double> quantityDictionary;
private Dictionary<string, double> QuantityDictionary
{
get { return quantityDictionary; }
set
{
quantityDictionary = value;
OnPropertyChanged("QuantityDictionary");
}
}
}
}
如果我设置quantityDictionary
为Public,我会收到一条错误消息“Object not set to an instance of an Object”。currentState.quantityDictionary.Clear();
我认为这是由于quantityDictionary
未正确初始化的面,但我不确定如何正确地做到这一点,或者是否需要在构造函数本身或其他地方完成。对此有点困惑...
有人可以帮我整理一下吗?谢谢
编辑 :
我已将该quantityDictionary
字段设置为Private,但如果它不是Public,那么我会CurrentState.quantityDictionary is inaccessible due to its protection level
在Main
课堂上得到一个currentState.quantityDictionary.Clear();
解决方案
推荐阅读
- .net - 分析明显的 .NET 1.1 Windows 服务挂起
- python - 在 Python 的 GEKKO 中实现宏观经济模型
- powershell - 支持管道的反向 TLS powershell
- python - 谷歌编码果酱 b 2019 年建设平原不断抛出运行时错误
- mongodb - 如果父对象为空,则 Cosmos DB 查询失败
- python - 即使终端允许,Pycharm 也不允许为 pygame 安装 pip
- python - python matplotlib底图在地图中显示多边形
- r - 使用 dplyr、filter、group_by 和 summarise 计算 R 中的平均天数?
- java - 通知 RecyclerView 中 Firestore 数据的变化
- c - 将字符串中的单词提取到动态二维字符数组中