2012-05-05 34 views
7

我开发在Windows 8的Visual Studio 11的应用程序,我想定义的事件处理程序,如下一DispatcherTimer实例:定义事件处理程序,在Windows DispatcherTimer的Tick事件8应用

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应用程序。

你能帮我吗?

回答

8

差不多已经有了:)你不需要实例化一个新的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 
     } 
    } 

试着读了各位代表了解事件Understanding events and event handlers in C#

+0

的变化,但我们不Windows应用程序有这个问题?我们要不要 ? –

+0

代理和事件处理程序在整个平台上都是相同的。这不是一个“问题”,它是如何工作:) – danielovich

+0

问题我的意思是'System.EventHandler ',我不是说这是一个错误 –

2

的WinRT使得使用泛型比标准.NET运行更多。 DispatcherTimer.Tick为defined in WinRT is here

public event EventHandler<object> Tick 

虽然WPF DispatcherTimer.Tick is here 公共事件的EventHandler蜱

还要注意的是,你不必使用标准的命名方法来创建一个事件处理程序。你可以使用lambda来做到这一点:

int timecounter = 10; 
DispatcherTimer timer = new DispatcherTimer(); 
public BlankPage() 
{ 
    this.InitializeComponent(); 

    timer.Tick += (s,o)=> 
    { 
     timecounter--; 
     if (timecounter == 0) 
     { 
      //disable all buttons here 
     } 
    }; 
} 
3

你的代码期望HandleTick有两个对象参数。不是一个对象参数和一个EventArg参数。

private void HandleTick(object s, object e) 

private void HandleTick(object s,EventArgs e) 

这是发生了针对Windows 8

相关问题