提高在派生类C#基类活动 [英] Raise Base Class Events in Derived Classes C#

查看:142
本文介绍了提高在派生类C#基类活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类DockedToolWindow:形式,许多类,从DockedToolWindow派生。我有一个持有并分配事件DockedToolWindow对象的容器类,但是我想要调用从子类的事件。

I have a base class DockedToolWindow : Form, and many classes that derive from DockedToolWindow. I have a container class that holds and assigns events to DockedToolWindow objects, however I want to invoke the events from the child class.

其实,我有一个关于如何落实的问题这是什么 MSDN网站是告诉我做的。下面这部分是给我的问题:

I actually have a question about how to implement what this MSDN site is telling me to do. This section below is giving me the problem:

    // 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);
        }
    }



当然这个例子编译和作品,但是当我更换ShapeChanged与移动(我从表格导出收购事件)时,它错误说我不能没有+ =或右侧移动 - =。我也删除了ShapeEventArgs通用标签。

Sure this example compiles and works, but when I replace "ShapeChanged" with "Move" (an event I acquired from deriving from Form), it errors saying I cannot have Move on the right side without += or -=. I also removed the ShapeEventArgs generic tags.

任何煽动为什么这不工作?什么是类中声明的事件和继承吗?之间的区别

Any incite on why this isn't working? What's the difference between an event declared within the class and one that is inherited?

推荐答案

您不能直接激发基类的事件。这就是为什么你必须让你的 OnShapeChanged 方法受保护的而不是私人的原因

You cannot directly fire base class events. This is exactly the reason why you had to make your OnShapeChanged method protected instead of private.

使用的 base.OnMove()代替

这篇关于提高在派生类C#基类活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆