在 Windows 8 应用程序中为 DispatcherTimer 的 Tick 事件定义事件处理程序 [英] defining event handler for Tick event of DispatcherTimer in windows 8 app
本文介绍了在 Windows 8 应用程序中为 DispatcherTimer 的 Tick 事件定义事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 Windows 8 Visual Studio 11 中开发一个应用程序,我想为 DispatcherTimer 实例定义一个事件处理程序,如下所示:
I am developing an application in windows 8 Visual studio 11, and I want to define an event handler for a DispatcherTimer instance as below:
public sealed partial class BlankPage : Page
{
int timecounter = 10;
DispatcherTimer timer = new DispatcherTimer();
public BlankPage()
{
this.InitializeComponent();
timer.Tick += new EventHandler(HandleTick);
}
private void HandleTick(object s,EventArgs e)
{
timecounter--;
if (timecounter ==0)
{
//disable all buttons here
}
}
.....
}
但我收到以下错误:
Cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<object>'
我是寡妇 8 个应用程序的新手开发者.
I am a novice developer to widows 8 apps.
你能帮我吗?
推荐答案
差不多了 :) 你不需要实例化一个新的 eventhandler 对象,你只需要指向处理事件的方法.因此,一个事件处理程序.
almost had it :) You don't need to instantiate a new eventhandler object, you only need to point to the method that handles the event. Hence, an eventhandler.
int timecounter = 10;
DispatcherTimer timer = new DispatcherTimer();
public BlankPage()
{
this.InitializeComponent();
timer.Tick += timer_Tick;
}
protected void timer_Tick(object sender, object e)
{
timecounter--;
if (timecounter == 0)
{
//disable all buttons here
}
}
尝试阅读代表以了解事件 了解事件和事件C#中的处理程序
Try to read up on delegates to understand events Understanding events and event handlers in C#
这篇关于在 Windows 8 应用程序中为 DispatcherTimer 的 Tick 事件定义事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文