本文介绍了无过载'方法'匹配委托“System.EventHandler”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个程序,一旦按钮被点击,每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,它显示错误:

解决方案

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

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

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

试试这个来代替:

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

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:

解决方案

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)
{
   ...
}

这篇关于无过载'方法'匹配委托“System.EventHandler”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:01