我正在努力将一些Events实施到正在处理的代码中,而正在使用的教科书中提供的示例却无法编译(C#Player手册,第3版)。我一直在尝试弄清我对事件/事件处理程序等没有了解的内容,但毫无用处。
我正在使用Microsoft Visual Studio(最新更新),这是我正在尝试进行的代码(我没有编写它-但是我认为弄清楚为什么它不起作用将对我有帮助。我遇到的其他事件问题)。
namespace EventsPractice
{
class Program
{
static void Main(string[] args)
{
Point point = new Point();
point.PointChanged += HandlePointChanged;
}
}
class Point
{
private double x;
private double y;
public double X
{
get { return x; }
set
{
x = value;
OnPointChanged();
}
}
public double Y
{
get { return y; }
set
{
y = value;
OnPointChanged();
}
}
public event EventHandler PointChanged;
public void OnPointChanged()
{
if (PointChanged != null)
PointChanged(this, EventArgs.Empty);
}
public void HandlePointChanged(object sender, EventArgs eventArgs)
{
// Do something Here
}
}
}
问题是在网上
point.PointChanged += HandlePointChanged;
我收到错误消息“名称HandlePointChanged在这种情况下不存在。”
我缺少明显的东西吗?为什么不起作用?
忽然笑
相关分类