簡體   English   中英

c#將事件上的參數從另一個類傳遞到主類

[英]c# Passing parameters on event from another class to main class

我有一個main(Form1)類,第二個類處理所有串行通信(ComPort)

當通過串口(Event)接收到新數據時,我想將它傳遞給Form1並對這些數據進行一些操作。 小例子:從接收的數據創建長字符串

Form1.cs的

public partial class Form1 : Form  
{  
//Creating instance of SerialCommunication.
....
....
string receivedString = ""; //Here I will store all data

public void addNewDataMethod(string newDataFromSerial)
{ 
  receivedString = receivedString + newDataFromSerial;
  MessageBox.Show(receivedString);
}

}

SerialCommunication.cs

public partial class SerialCommunicationPort   
    {  
      constructor()  
      {      
       .... //open serial port with the relevant parameters  
       ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPortBufferRead);//Create Handler  
      }  
     public void ComPortBufferRead(object sender, SerialDataReceivedEventArgs e)  
     {  
      //create array to store buffer data  
      byte[] inputData = new byte[ComPort.BytesToRead];  
      //read the data from buffer  
      ComPort.Read(inputData, 0, ComPort.BytesToRead);  

    //*****  What should I write here in order to  pass "inputData" to Form1.addNewDataMethod ?


    }  
  }

我試過以下幾點:

Form1 Form;  
Form1.addNewDataMethod(inputData.ToString());

上面的代碼將生成一個錯誤:使用未分配的局部變量“Form”

Form1 Form1 = new Form1(); 
Form1.addNewDataMethod(inputData.ToString());

以上將創建Form1的新實例,並且不包含以前接收的數據。

有什么建議 ?

SerialCommunication類中創建一個事件,該事件將在數據到達時引發:

public partial class SerialCommunicationPort   
{ 
    public event Action<byte[]> DataReceived;

    public void ComPortBufferRead(object sender, SerialDataReceivedEventArgs e)  
    {       
       byte[] inputData = new byte[ComPort.BytesToRead];  
       ComPort.Read(inputData, 0, ComPort.BytesToRead);  

       if (DataReceived != null)
           DataReceived(inputData);
    }  
}

然后在表單中訂閱此活動。

public partial class Form1 : Form  
{  
    SerialCommunication serialCommunication;

    public Form1()
    {
        InitializeComponent();
        serialCommunication = new SerialCommunication();
        serialCommunication.DataReceived += SerialCommunication_DataReceived;
    } 

    private void SerialCommunication_DataReceived(byte[] data)
    {
        // get string from byte array 
        // and call addNewDataMethod
    }
}

如果要遵循WinForms的Microsoft編碼指南,則應使用EventHandler<DataReceivedEventArgs>委托而不是Action<byte[]> EventHandler<DataReceivedEventArgs>委托,其中DataReceivedEventArgs是從EventArgs繼承的類。

可能最好的方法是使用事件。 定義一個EventArgs參數將采用字符串的事件。 在Form1中訂閱該事件,並在您的串行通信類中,只觸發將您想要的字符串傳遞給eventargs的事件。

你想要的方式應該也可以,但是這樣的事情:

Form1 Form = new Form1();  
Form.Show(); // to display it...
/// to other stuff...
Form.addNewDataMethod(inputData); // call the method on the INSTANCE which you opened!

類似於方法的用法,您可以在Form1中定義一個屬性:

private string _myString = "";
public string MyString {
    private get {   
}
set {
    string _myString= value;
   // to with the string what you want
}
}

然后類似於方法調用使用

Form.MyString = "abc";

希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM