<i id='ZrEVV'><tr id='ZrEVV'><dt id='ZrEVV'><q id='ZrEVV'><span id='ZrEVV'><b id='ZrEVV'><form id='ZrEVV'><ins id='ZrEVV'></ins><ul id='ZrEVV'></ul><sub id='ZrEVV'></sub></form><legend id='ZrEVV'></legend><bdo id='ZrEVV'><pre id='ZrEVV'><center id='ZrEVV'></center></pre></bdo></b><th id='ZrEVV'></th></span></q></dt></tr></i><div id='ZrEVV'><tfoot id='ZrEVV'></tfoot><dl id='ZrEVV'><fieldset id='ZrEVV'></fieldset></dl></div>
      <bdo id='ZrEVV'></bdo><ul id='ZrEVV'></ul>

    <small id='ZrEVV'></small><noframes id='ZrEVV'>

  1. <tfoot id='ZrEVV'></tfoot>
    <legend id='ZrEVV'><style id='ZrEVV'><dir id='ZrEVV'><q id='ZrEVV'></q></dir></style></legend>

      通过 EventHandler 传回返回值

      时间:2023-09-15
      <i id='xf9KM'><tr id='xf9KM'><dt id='xf9KM'><q id='xf9KM'><span id='xf9KM'><b id='xf9KM'><form id='xf9KM'><ins id='xf9KM'></ins><ul id='xf9KM'></ul><sub id='xf9KM'></sub></form><legend id='xf9KM'></legend><bdo id='xf9KM'><pre id='xf9KM'><center id='xf9KM'></center></pre></bdo></b><th id='xf9KM'></th></span></q></dt></tr></i><div id='xf9KM'><tfoot id='xf9KM'></tfoot><dl id='xf9KM'><fieldset id='xf9KM'></fieldset></dl></div>

        <small id='xf9KM'></small><noframes id='xf9KM'>

      • <legend id='xf9KM'><style id='xf9KM'><dir id='xf9KM'><q id='xf9KM'></q></dir></style></legend>
        <tfoot id='xf9KM'></tfoot>

                <tbody id='xf9KM'></tbody>

                <bdo id='xf9KM'></bdo><ul id='xf9KM'></ul>
                本文介绍了通过 EventHandler 传回返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试写入 API,当我从表中获取数据时,我需要调用事件处理程序.像这样的:

                Im trying to write to an API and I need to call an eventhandler when I get data from a table. Something like this:

                    public override bool Run(Company.API api)
                    {
                        SomeInfo _someInfo = new SomeInfo();
                
                        if (_someInfo.Results == 1)
                            return true;
                        else
                            return false;
                
                        using (MyTable table = new MyTable(api))
                        {
                            table.WhenData += new EventHandler<DataEventArgs<Record>>(table_WhenData);
                            table.WhenDead += new EventHandler<EventArgs>(table_WhenDead);
                            table.Start();
                        }
                
                    public void table_WhenData(object sender, DataEventArgs<Record> e)
                    {
                        return true;
                    }
                

                我遇到的问题是我不知道如何将返回值从 table_WhenData 传递回 Run 方法.

                The problem that Im having is I dont know how to pass a return value back from table_WhenData to the Run method.

                我尝试了很多方法(例如尝试将 _someInfo 传递给方法),但我似乎无法正确使用语法.

                Ive tried many ways (like trying to pass _someInfo to the method) but I just cant seem to get the syntax right.

                非常感谢任何建议.

                推荐答案

                这里的常见模式是不从事件处理程序返回任何数据,而是向您的事件参数对象添加属性,以便事件的使用者可以设置然后调用者可以访问的属性.这在 UI 处理代码中很常见;你到处都可以看到 Cancel 事件的概念.

                The common pattern here is not to return any data from the event handler, but to add properties to your event argument object so that the consumer of the event can set the properties which the caller can then access. This is very common in UI handling code; you see the Cancel event concept all over the place.

                以下是伪代码,尚未编译.它的目的是展示模式.

                The following is pseudo code, and not compile ready. Its intent is to show the pattern.

                public class MyEventArgs : EventArgs
                {
                   public bool Cancel{get;set;}
                }
                
                public bool fireEvent()
                {
                    MyEventArgs e=new MyEventArgs();
                
                    //Don't forget a null check, assume this is an event
                    FireEventHandler(this,e);
                
                    return e.Cancel;
                }
                
                public HandleFireEvent(object sender, MyEventArgs e)
                {
                 e.Cancel=true;
                }
                

                编辑

                我喜欢 Jon Skeet 的措辞:使 EventArgs 可变.也就是说,事件的使用者可以修改 EventArgs 对象的状态,从而允许事件的引发者获取该数据.

                I like how Jon Skeet worded this: make the EventArgs mutuable. That is, the consumer of the event can modify the state of the EventArgs object allowing for the raiser of the event to get to that data.

                这篇关于通过 EventHandler 传回返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 C# 中转发事件 下一篇:Winforms 用户控件自定义事件

                相关文章

              • <tfoot id='Ium3x'></tfoot>
                  <bdo id='Ium3x'></bdo><ul id='Ium3x'></ul>

                  <i id='Ium3x'><tr id='Ium3x'><dt id='Ium3x'><q id='Ium3x'><span id='Ium3x'><b id='Ium3x'><form id='Ium3x'><ins id='Ium3x'></ins><ul id='Ium3x'></ul><sub id='Ium3x'></sub></form><legend id='Ium3x'></legend><bdo id='Ium3x'><pre id='Ium3x'><center id='Ium3x'></center></pre></bdo></b><th id='Ium3x'></th></span></q></dt></tr></i><div id='Ium3x'><tfoot id='Ium3x'></tfoot><dl id='Ium3x'><fieldset id='Ium3x'></fieldset></dl></div>

                    <legend id='Ium3x'><style id='Ium3x'><dir id='Ium3x'><q id='Ium3x'></q></dir></style></legend>
                  1. <small id='Ium3x'></small><noframes id='Ium3x'>