无过载'方法'匹配委托“System.EventHandler”方法、EventHandler、System

2023-09-03 06:24:20 作者:野心未泯.

我想建立一个程序,一旦按钮被点击,每5第二个将执行的功能(OnTimed)。

下面是code到目前为止:

 私人无效bntCapture_Click(对象发件人,RoutedEventArgs E)
{
    DispatcherTimer T1 =新DispatcherTimer();
    t1.Interval = TimeSpan.FromMilliseconds(5000);
    t1.IsEnabled = TRUE;
    t1.Tick + =新的EventHandler(OnTimed);
    t1.Start();
}

无效OnTimed(对象发件人,ElapsedEventArgs E)
{

    imgCapture.Source = imgVideo.Source;
    System.Threading.Thread.Sleep(1000);
    Helper.SaveImageCapture((的BitmapSource)imgCapture.Source);
}
 

当我运行code,它显示错误:

  

不超载的'方法'匹配委托System.EventHandler

解决方案

事件处理程序方法的签名是不符合委托类型兼容。

Subsribers的DispatcherTimer.Tick事件必须是EventHandler委托类型,其声明为:

 公共委托无效事件处理程序(对象发件人,EventArgs e)条;
 

试试这个来代替:

 无效OnTimed(对象发件人,EventArgs的)
{
   ...
}
 
C 无能委托

I am trying to build a program that, once the button was click, every 5 second will perform the function (OnTimed).

Below is the code so far:

private void bntCapture_Click(object sender, RoutedEventArgs e)
{ 
    DispatcherTimer t1 = new DispatcherTimer();
    t1.Interval = TimeSpan.FromMilliseconds(5000);
    t1.IsEnabled = true;
    t1.Tick += new EventHandler(OnTimed);
    t1.Start();
}

void OnTimed(object sender, ElapsedEventArgs e)
{

    imgCapture.Source = imgVideo.Source;
    System.Threading.Thread.Sleep(1000);
    Helper.SaveImageCapture((BitmapSource)imgCapture.Source);
} 

When i run the code, it show the error:

"No overload for 'method' matches delegate 'System.EventHandler'

解决方案

The signature of the event-handler method isn't compatible with the delegate type.

Subsribers to the DispatcherTimer.Tick event must be of the EventHandler delegate type, which is declared as:

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

Try this instead:

void OnTimed(object sender, EventArgs e)
{
   ...
}