using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Cls : INotifyPropertyChanged
{
private string my;
public string MyProperty
{
get
{
return my;
}
set
{
my = value;
PropertyChanged(this, new PropertyChangedEventArgs("MyProperty"));
}
}
public Cls()
{
MyProperty = "Hello";
}
public void print()
{
Console.WriteLine(MyProperty);
}
protected virtual void OnPropertyChanged(string name)
{
}
public event PropertyChangedEventHandler PropertyChanged;
}
class Program
{
static void Main(string[] args)
{
Cls s = new Cls();
s.print();
}
}
}
当我运行这段代码时,它会显示:
未经处理的异常: System.NullReferenceException: 对象引用未设置到对象的实例。
当我不使用
INotifyPropertyChanged
时,它能够正常工作。我不理解问题的原因。
MyProperty = "Hello";
- Pranit Kothari