Gam*_*ure 8 c# events base-class
我有一个基类DockedToolWindow:Form,以及从DockedToolWindow派生的许多类.我有一个容器类,用于保存和分配事件到DockedToolWindow对象,但是我想调用子类中的事件.
我实际上有一个关于如何实现这个MSDN网站告诉我要做的事情的问题.以下这节给我的问题是:
// The event. Note that by using the generic EventHandler<T> event type
// we do not need to declare a separate delegate type.
public event EventHandler<ShapeEventArgs> ShapeChanged;
public abstract void Draw();
//The event-invoking method that derived classes can override.
protected virtual void OnShapeChanged(ShapeEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<ShapeEventArgs> handler = ShapeChanged;
if (handler != null)
{
handler(this, e);
}
}
Run Code Online (Sandbox Code Playgroud)
当然这个例子编译和工作,但是当我用"Move"(我从Form派生获得的事件)替换"ShapeChanged"时,它错误地说我不能在没有+ =或 - =的情况下移动右侧.我还删除了ShapeEventArgs通用标记.
任何煽动为什么这不起作用?在类中声明的事件和继承的事件之间有什么区别?
Gro*_*roo 12
您无法直接触发基类事件.这正是你必须用你的OnShapeChanged
方法protected
代替的方法private
.
请改用base.OnMove().
归档时间: |
|
查看次数: |
12959 次 |
最近记录: |