【问题标题】:Add delegate or event property to this class?向此类添加委托或事件属性?
【发布时间】:2010-10-04 13:56:45
【问题描述】:

我创建了一个控件,其他开发人员可以在其中创建实例并使用。控件中有一个按钮单击。我如何允许开发人员在我的控件的某个部分插入他们自己的代码?我不确定在这种情况下是否或如何准确使用委托或事件。有人可以在下面的示例中提供帮助吗:

public class MyControl : CompositeControl
{
   ...
   void btnSubmit_Click(object sender, EventArgs e)
   {
      //DO SOMETHING
      if (success)
      {
         //CALL DEVELOPER'S LOGIC
      }
   }
}

在开发者代码中,当控件的按钮点击成功时,如何传入他们的逻辑?

protected override void CreateChildControls()
{
    var ctlMyControl = new MyControl();

    //ADD SuccessCode() TO ctlMyControl
    //SO IT IS TRIGGERED ON SUCCESS
    //MAYBE SOMETHING LIKE:
    //ctlMyControl.SuccessEvent = SuccessCode()??

    this.Control.Add(ctlMyControl);
}

protected void SuccessCode()
{
    //DO SOMETHING
}

如何更新 MyControl 以允许这样做?

【问题讨论】:

    标签: c# .net asp.net events delegates


    【解决方案1】:

    您需要在控件中添加event,如下所示:

    void btnSubmit_Click(object sender, EventArgs e) {
        //DO SOMETHING
        if (success) {
            OnSomethingCompleted();
        }
    }
    
    ///<summary>Occurs when the operation is successfully completed.</summary>
    public event EventHandler SomethingCompleted;
    ///<summary>Raises the SomethingCompleted event.</summary>
    internal protected virtual void OnSomethingCompleted() { OnSomethingCompleted(EventArgs.Empty); }
    ///<summary>Raises the SomethingCompleted event.</summary>
    ///<param name="e">An EventArgs object that provides the event data.</param>
    internal protected virtual void OnSomethingCompleted(EventArgs e) {
        if (SomethingCompleted != null)
            SomethingCompleted(this, e);
    }
    

    在表单中,您可以为事件添加处理程序,如下所示:

    myControl.SomethingCompleted += myControl_SomethingCompleted;
    
    void myControl_SomethingCompleted(object sender, EventArgs e) {
        //Do things
    }
    

    有关详细信息,请参阅documentation

    【讨论】:

    • 我相信 EventHandler 的签名是 (object sender, EventArgs e) 但您的处理程序只有 (EventArgs e) 的签名。我认为这不会按原样工作??
    • @Doug:这确实有效。仔细阅读;那不是处理程序。
    • 哈,是的,我确实看错了 - 感谢下面的提示,顺便说一句,我没有意识到 EventArgs 类中有一个静态的“空”成员。
    【解决方案2】:

    由于您的事件使用 EventHandler 类型的默认签名,您只需公开一个 EventHandler 类型的公共事件。所以看看下面的代码吧。

    这是您实施新事件的方法!

    public class MyControl : CompositeControl
    {
       //New public event declaration of type EventHandler
       public event EventHandler Submit_OnClick;
    
       void btnSubmit_Click(object sender, EventArgs e)
       {
          //DO SOMETHING
          if (success)
          {
             //CALL DEVELOPER'S LOGIC
    
             //Code to raise the event only if there are subscribers.
             if (Submit_OnClick != null)
             {
               Submit_OnClick(this, new EventArgs());
             }
          }
       }
    }
    

    这是您使用新活动的方法!

    protected override void CreateChildControls()
    {
        var ctlMyControl = new MyControl();
          //Subscribed to the newly added event of your MyControl class
            ctlMyControl.Submit_OnClick += new EventHandler(SuccessCode);
    
        //ADD SuccessCode() TO ctlMyControl
        //SO IT IS TRIGGERED ON SUCCESS
        //MAYBE SOMETHING LIKE:
        //ctlMyControl.SuccessEvent = SuccessCode()??
    
        this.Control.Add(ctlMyControl);
    }
    
    //Modified the signature to be compliant with EventHandler type
    protected void SuccessCode(object sender, EventArgs e)
    {
        //DO SOMETHING
    }
    

    享受吧!

    【讨论】:

      【解决方案3】:

      您需要声明事件,并在必要时创建自定义事件参数类来传递有关事件的信息。

      这里是a blog post我写了一段时间,介绍了整个过程。

      【讨论】:

        【解决方案4】:

        您应该向您的控件添加一个事件。

        类似这样的:

        public class MyControl : CompositeControl
        {
               public event EventHandler Success;
               ...
               void btnSubmit_Click(object sender, EventArgs e)
           {   
               //DO SOMETHING
                if (success)
                {
                     //CALL DEVELOPER'S LOGIC
                     OnSuccess();
                }
           }
        
            private static void OnSuccess()
            {
                 if(Success != null)
                 {
                      Success(this, EventArgs.Empty); //or you can pass event args you want. But you should use EventHandler<T> instead.
                 }
            }
        }
        

        与其他开发人员相比,您可以像这样使用您的控件:

        protected override void CreateChildControls()
        {
            var ctlMyControl = new MyControl();
        
            ctlMyConstrol += SuccessCode;
        
            this.Control.Add(ctlMyControl);
        }
        
        protected void SuccessCode(object sender, EventArgs e)
        {
            //DO SOMETHING
        }
        

        您可以在 MSDN 上阅读有关事件的更多信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-14
          • 2019-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多