5

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

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

发送接口

interface ISendData
{
  void Send();
}

Tcp 连接类

//Need some type of delegate??

public class TCPconnection : ISendData
{
   void Send()
   {
     //how invoke/fire a send Event?
   }
}

UDP 连接类

//Need some type of delegate??

public class UDPConnection : ISendData
{
   void Send()
   {
     //how invoke/fire a send event?
   }
}

“应该”订阅查看触发事件的 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;
   }

}

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

 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);
            }
        }
     }
4

2 回答 2

3

这是一个简单的示例,说明如何实现发送和不发送的事件并订阅它

public class Connection
{
    //Set up an event
    public event EventHandler DataSending;
    public event EventHandler DataNotSending

    //This method will trigger the event for sending
    private void OnDataSending()
    {
        if (DataSending!= null) { DataSending(this, EventArgs.Empty); }
    }

    //this method will trigger the event for finished sending
    private void OnDataNotSending()
    {
        if (DataNotSending!= null) { DataNotSending(this, EventArgs.Empty); }
    }

    //This method performs your send logic
    public void Send()
    {
         //Call your method that tells the event to be raised
         OnDataSending();
         //Then put your send code
         OnDataNotSending(); //we're done!

    }
}

这是您在消费程序中使用它的方式

public class myForm
{
     //This method is the one that sets up the 
     //instance and subscribes to the event
     public myForm()
     {
         Connection con = new Connection();

         con.DataSending += new EventHandler(con_DataSending);
         con.DataNotSending += new EventHander(con_DataNotSending);

     }

     void con_DataSending(object sender, EventArgs e)
     {
         //Put your subscription logic here.
         //Whatever you want to do in response to a send
     }

     void con_DataNotSending(object sender, EventArgs e)
     {
         //Put your subscription logic here.
         //Respond to it not sending
     }
}
于 2013-03-17T01:27:55.863 回答
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 ... //

}

现在 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);
    }

}

这是您的表单的设置。

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;
            }
        };
    }
}

希望这可以帮助。

于 2013-03-17T01:51:44.033 回答