1

我有以下界面:

public interface IModuleTile
{
    void AddEvent(/*Type here*/ methodToAdd);
    void RemoveEvent(/*Type here*/ methodToRemove);
}

我想这样做:

public partial class TestControl : UserControl, IModuleTile
{
    public TestControl()
    {
        InitializeComponent();
    }

    public void AddEvent(/*Type here*/ eventToAdd)
    {
        ShowModule.Click += methodToAdd;
    }

    public void RemoveEvent(/*Type here*/ methodToRemove);
    {
        ShowModule.Click += methodToRemove;
    }
}

我必须将什么设置为传递方法的接口类型?

4

1 回答 1

2

我在这里要做的只是将一个事件直接放入接口中,而不是显式添加添加/删除方法。

public interface IModuleTile
{ 
    //change `EventHandler` to match whatever the event handler type 
    //is for the event that you're "wrapping", if needed
    event EventHandler MyClick;
}

然后实现可以是这样的:

public partial class TestControl : UserControl, IModuleTile
{
    //You'll need to change `EventHandler` here too, if you changed it above
    public event EventHandler MyClick
    {
        add
        {
            ShowModule.Click += value;
        }
        remove
        {
            ShowModule.Click -= value;
        }
    }
}
于 2012-08-29T14:24:52.903 回答