简体   繁体   中英

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

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.

but then i got this error 在此处输入图片说明

---Update--- Eventually I found that actually my scenario doesn't require to use something like 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.

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)

2 - The assembly containing Common.WinLose_ProgressStage is not referenced.

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 . It can therefore find a suitable overload of Append_UC_Progess and it all resolves ok.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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