为什么我的事件是NULL,即使我从另一个表单订阅c#

时间:2014-06-23 12:18:13

标签: c# events delegates

我是c#编程的新手,我对我创建的事件有疑问。

我有一个名为Serial的女士,我做了一个事件:

public delegate void dOnPreAmpFrequencyUpdate(object sender, double frequency);
public event dOnPreAmpFrequencyUpdate OnPreAmpFrequencyUpdate;

我希望从串行方法中提取事件,如下所示:

if (OnPreAmpFrequencyUpdate!=null)
{
    this.OnPreAmpFrequencyUpdate(this, iWorkingParameters.Frequency);
}

在我的主要表格中,我订阅了这个活动:

public static Serial _serial;
public frmMain()
{
   _serial = new Serial();
   _serial.OnPreAmpFrequencyUpdate += new Serial.dOnPreAmpFrequencyUpdate(_serial_OnPreAmpFrequencyUpdate);

   InitializeComponent();

}

void _serial_OnPreAmpFrequencyUpdate(object sender, double frequency)
{ 
   if (this.IsDisposed) return;
   updatePreAmpFrequency(frequency, null);
}

private void updatePreAmpFrequency(object sender, EventArgs e)
{
   //update the pre-amp frequency, according to the value
   try
   {
      if (this.InvokeRequired)
      {
         EventHandler ev = new EventHandler(updatePreAmpFrequency);
         this.Invoke(ev, sender, null);
         return;
      }

      double frequency = (double)sender;
      numFrequency.Value = (decimal)frequency;
      numFrequency.ForeColor = Color.Black;
   }
   catch (Exception)
   { }
}

我无法理解为什么当我尝试在我的Serial类中提升我的事件时,即使我将它订阅到我的主窗体中它也总是为空。

有人有任何想法吗?

0 个答案:

没有答案