调度计时器不起作用 [英] Dispatch timer won't work

查看:39
本文介绍了调度计时器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚调度计时器是如何工作的,以便我可以将它实现到我的程序中,我按照网站上的确切说明进行操作,并寻找有关堆栈溢出的答案.人们说他们的问题已经解决,但我有非常相似的代码,它不会工作......

I'm trying to figure out how a dispatch timer works so I can implement it into my program, I followed the exact instructions on a website and looked for answers on stack overflow. People said their problem was fixed but I have very similar code and it wont work...

错误是:

timer_Tick"没有重载匹配委托EventHandler"

No overload for "timer_Tick" matches delegate "EventHandler<object>"

我能做什么?

public MainPage()
{
    this.InitializeComponent();

    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(1);
    timer.Tick += timer_Tick;
    timer.Start();
}

void timer_Tick(EventArgs e)
{
    TimeRefresh();
}

推荐答案

您需要修复事件处理程序签名.它缺少发送者,第二个参数的类型只是 object.(请参阅文档.)

You need to fix the event handler signature. It's missing the sender and the type of the second parameter is just object. (See the documentation.)

void timer_Tick(object sender, object e)
{
    TimeRefresh();
}

您还需要在类的顶部添加一个 using Windows.UI.Xaml;,或者使用完整的命名空间实例化计时器:

You also need to add a using Windows.UI.Xaml; to the top of your class, or instantiate the timer using the full namespace:

Windows.UI.Xaml.DispatcherTimer timer = new Windows.UI.Xaml.DispatcherTimer();

<小时>

如果有人偶然发现并使用 WPF,它有自己的 DispatchTimer.确保您引用的是WindowsBase"(默认情况下应该在那里).签名略有不同.


If anyone stumbles on this and is using WPF, it has it own DispatchTimer. Make sure you're referencing "WindowsBase" (should be there by default). The signature is slightly different.

void timer_Tick(object sender, EventArgs e)
{
    TimeRefresh();
}

它所在的命名空间也不同.要么将 using System.Windows.Threading; 添加到顶部,要么使用完整的命名空间进行限定:

The namespace it lives in is different too. Either add using System.Windows.Threading; to the top, or qualify with the full namespace:

System.Windows.Threading.DispatcherTimer timer
    = new System.Windows.Threading.DispatcherTimer();

<小时>

如果您使用的是 WinForms,则需要使用不同的计时器.阅读本文了解 WinForms Timer 和 WPF DispatchTimer 之间的区别.


If you're using WinForms, you want to use a different timer. Read this for the difference between the WinForms Timer and the WPF DispatchTimer.

这篇关于调度计时器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆