方法“xxx"不能是事件的方法,因为此类派生自的类已经定义了该方法 [英] The method 'xxx' cannot be the method for an event because a class this class derives from already defines the method

查看:22
本文介绍了方法“xxx"不能是事件的方法,因为此类派生自的类已经定义了该方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码:

public class Layout : UserControl
{    
    protected void DisplayX_DisplayClicked(object sender, DisplayEventArgs e)
    {
        CurrentDisplay = (CameraPanel)sender;
    }
}

'Layout' 是我其他布局的基类.例如,我有一个从基类 'Layout' 派生的 'Layout1'.Layout1 有一个元素 Display01.Display01 有一个 DisplayClicked 事件.我正在尝试通过 Visual Studio 设计器将 DisplayX_DisplayClicked 分配给 Display01 的 DisplayClicked 事件.

'Layout' is a base class for my other layouts. For example, I have a 'Layout1' derived from base class 'Layout'. Layout1 has an element Display01. Display01 has an DisplayClicked event. I'm trying to assign DisplayX_DisplayClicked via Visual Studio Designer to DisplayClicked event of Display01.

public partial class Layout1 : Layout
{
    private CameraPanel Display01;
}

它给了我一个错误:

方法xxx"不能是事件的方法,因为此类派生自的类已经定义了该方法.

如何使用基类的方法作为派生类的事件处理程序?是否可以 ?如果是,如何.如果没有,为什么.

How to use method from base class as a eventhandler of derived class ? Is it possible ? If so, how. If no, why.

推荐答案

设计人员无法处理,但您可以在代码中很好地完成.在 Layout1 的构造函数中,只需编写:

The designer can't handle that, but you can do it in code just fine. In the constructor for Layout1, just write:

public Layout1()
{   
    InitializeComponent();
    this.Display01.DisplayClicked += base.DisplayX_DisplayClicked;
}

或者,您可以让设计器生成一个名为 Display01_DisplayClicked 的方法,然后将其实现为:

Alternately, you could let the designer generate a method named Display01_DisplayClicked, and then implement it as:

private void Display01_DisplayClicked(object sender, DisplayEventArgs e)
{
    base.DisplayX_DisplayClicked(sender, e);
}

那比较冗长,所以我会采用第一种方式,但它会让设计者意识到该事件有一个处理程序.

That's a lot more verbose, so I would do it the first way, but it would let the designer be aware that there was a handler for that event.

这篇关于方法“xxx"不能是事件的方法,因为此类派生自的类已经定义了该方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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