简体   繁体   English

C#自定义EventHandler

[英]C# Custom EventHandler

i have a custom event handler in the page, and it is called by the user controls of it. 我在页面中有一个自定义事件处理程序,它由用户控件调用。

everything is fine, except there is error display in the code ( red highlighted), but the program can be compiled, and able to run with no apparent error. 一切正常,除了代码中有错误显示(红色突出显示)之外,但该程序可以编译,并且可以在没有明显错误的情况下运行。

but i want to fix (or understand) the reason why the visual studio showed error for that 但我想修复(或理解)Visual Studio对此显示错误的原因

the error is 错误是 在此处输入图片说明

the code is 该代码是

-PAGE -页

Operator_agentcontrol2 agentcontrol = (Operator_agentcontrol2)Page.LoadControl("~/operator/agentcontrol2.ascx");
agentcontrol.displayLevel = (int)Common.WinLose_Level.lvChild4 + 10 + (Panel_agents.Controls.Count * 10);
agentcontrol.AppendProcess += Append_UC_Progress;//Error line

the event in the page- 页面中的事件-

public void Append_UC_Progress(object sender, EventArgs e)
{
        Common.WinLose_ProgressStage wps = (Common.WinLose_ProgressStage)e;
        progress.AppendProgress(wps);
        SaveProgressVS();
}

-USER CONTROL -用户控制

public partial class Operator_agentcontrol2 : System.Web.UI.UserControl
{
    public event EventHandler<Common.WinLose_ProgressStage> AppendProcess;
}

Thanks 谢谢

---Update--- I have tried to follow https://msdn.microsoft.com/en-us/library/db0etb8x(v=vs.85).aspx for the custom event handler. ---更新---我已尝试遵循https://msdn.microsoft.com/zh-cn/library/db0etb8x(v=vs.85).aspx作为自定义事件处理程序。

but then i got this error 但后来我得到了这个错误 在此处输入图片说明

---Update--- Eventually I found that actually my scenario doesn't require to use something like EventHandler ---更新---最终我发现实际上我的场景不需要使用诸如EventHandler之类的东西

i have changed the code in user control 我已经更改了用户控件中的代码

public partial class Operator_agentcontrol2 : System.Web.UI.UserControl
{
    public event EventHandler AppendProcess;
}

By doing this the error is gone, and the user control still able to call the Page's function successfully with an object Common.WinLose_ProgressStage. 这样,错误消失了,用户控件仍然可以使用对象Common.WinLose_ProgressStage成功调用Page的函数。

As far as I can see, two errors are being reported... 据我所知,正在报告两个错误...

1 - It cannot find a suitable overload for Append_UC_Progess (which takes a Common.WinLose_ProgressStage as an argument) 1 -它不能找到一个合适的过载Append_UC_Progess (这需要一个Common.WinLose_ProgressStage作为参数)

2 - The assembly containing Common.WinLose_ProgressStage is not referenced. 2-包含Common.WinLose_ProgressStage的程序集未被引用。

What I would suggest is happening, is that once it is all compiled the assembly containing Common.WinLose_ProgressStage gets pulled in (perhaps by another referenced assembly), and thus it is noticed that it inherits from EventArgs . 我建议发生的事情是,一旦全部编译完,包含Common.WinLose_ProgressStage的程序集就会被拉入(也许被另一个引用的程序集)拉入,因此注意到它继承自EventArgs It can therefore find a suitable overload of Append_UC_Progess and it all resolves ok. 因此,它可以找到合适的Append_UC_Progess重载,并且一切正常。

In order to get rid of the error, I would suggest explicitly referencing the assembly containing Common.WinLose_ProgressStage , so that Visual Studio can see the inheritance tree at design time. 为了消除该错误,我建议显式引用包含Common.WinLose_ProgressStage的程序集,以便Visual Studio在设计时可以看到继承树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM