单击按钮时如何使用私有区域的数据

How can I use the data in private area when I click the button

我正在尝试从私有 class 获取字节并在 btn.click.What 中使用它,我应该这样做吗?

示例代码如下:


private void CameraConnnection_DataReceived(object sender, BReceivedData x)
        {
            byte[] data = e.ReceivedData;
        }

        private void BtnGetImage_Click(object sender, EventArgs e)
        {
            unsafe
            {
               byte a[]= e.ReceivedData;// This is not working how can I get received data from there.There is a event and it's triggering from there.
            }

由于 EventArgs class 没有 ReceivedData 字段或 属性,下面的代码 无法编译 :

private void BtnGetImage_Click(object sender, EventArgs e)
{
  ...
  // Compile time error: ReceivedData doesn't exist
  byte a[] = e.ReceivedData;
  

您可以在 DataReceived 事件中获得并 保存 ReceivedData 但在 Click:

中使用它
private byte[] receivedData = new byte[0];

private void CameraConnnection_DataReceived(object sender, BReceivedData x)
{
    // ?? new byte[0] - to be on the safe side and never have null
    receivedData = e.ReceivedData ?? new byte[0];
}

private void BtnGetImage_Click(object sender, EventArgs e) {
  byte[] a = receivedData;

  ...
}