如何跨不同类发布和订阅事件

sta*_*low 5 c# events delegates

目标:在 udp 或 tcp 使用其发送方法时更改表单上的图像

问题:我不知道如何正确设置事件、事件处理程序和委托

发送接口

interface ISendData
{
  void Send();
}
Run Code Online (Sandbox Code Playgroud)

TCP连接类

//Need some type of delegate??

public class TCPconnection : ISendData
{
   void Send()
   {
     //how invoke/fire a send Event?
   }
}
Run Code Online (Sandbox Code Playgroud)

UDP 连接类

//Need some type of delegate??

public class UDPConnection : ISendData
{
   void Send()
   {
     //how invoke/fire a send event?
   }
}
Run Code Online (Sandbox Code Playgroud)

“应该”订阅查看已触发事件的 winform

public class myForm
{
   private DataWatcher datawatcher = new DataWatcher();
   private Image statusIndicator = null;

   public myform()
   {
     initComponents();

     datawatcher.DataSendActive += new DataWatcherSendHandler(DataSending);
     datawatcher.DataSendInactive += new DataWatcherSendHandler(NoDataSending);
   }

   public void DataSending(object sender, DataWatcherArgs e)
   {
      statusIndicator = Properties.resources.greenLight;
   }

   public void NoDataSending(object sender, DataWatcherArgs e)
   {
      statusIndicator = Properties.resources.redLight;
   }

}
Run Code Online (Sandbox Code Playgroud)

事件/事件处理程序??但我真的不知道我在这里做什么来完成这项工作

 public delegate void EventHandler(object sender, EventArgs e);

    class DataWatcher
    {
        public event EventHandler DataSendActive;
        public event EventHandler DataSendInactive;

        protected virtual void onDataSendActive(System.EventArgs e)
        {
            if (DataSendActive != null)
            {
                DataSendActive(this, e);
            }
        }
        protected virtual void onDataSendInactive(System.EventArgs e)
        {
            if (DataSendInactive != null)
            {
                DataSendInactive(this, e);
            }
        }
     }
Run Code Online (Sandbox Code Playgroud)

Mad*_*han 3

有许多约定可以用来做到这一点。这是我的小实现。

public enum ActivityState
{
    Sending,
    Receiving,
    Idle
}

public interface IDataTransferManager
{
    // This event will fire when the activity state changes.
    // note that Action<T> is introduced in .NET 3.5
    // if you're using .NET 2.0, you can use a delegate.
    event Action<ActivityState> DataActivityStateChange;
   

    void Send(byte[] data);
    //byte[] Receive(); 
    // ... more methods ... //

}
Run Code Online (Sandbox Code Playgroud)

现在 TcpConnection 类将实现此功能。

public class TcpConnection : IDataTransferManager
{
    public event Action<ActivityState> DataActivityStateChange;

    public void Send(byte[] data)
    {
        // we're sending data. fire the change event
        FireDataActivityStateChange(ActivityState.Sending);

        //TODO: send the data

        // we're done sending. Fire the change event
        FireDataActivityStateChange(ActivityState.Idle);
    }



    private void FireDataActivityStateChange(ActivityState state)
    {
        // helper method, so I don't have to check the event 
        // to avoid null reference exceptions.
        if (DataActivityStateChange != null)
            DataActivityStateChange(state);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是您的表单的设置。

class MyForm // :Form
{
    IDataTransferManager dataManager;

    public MyForm()
    {   // here, usually an instance will be passed in, 
        // so there's only one instance throughout the application.
        // let's new up an instance for explanation purposes.
        dataManager = new TcpConnection();

        dataManager.DataActivityStateChange += (state) => 
        {
            // NOTE: if you don't like inline, 
            // you can point this labda to a method.
            
            switch (state)
            {
                case ActivityState.Sending:
                    // change the image to the spinning toilet ball
                    break;
                case ActivityState.Receiving:
                    // change the image to the spinning toilet ball, but reverse :P
                    break;
                case ActivityState.Idle:
                    // hide it ?
                    break;
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)