无法将类型System.EventHandler隐式转换为System.EventHandler< object>.错误

编程入门 行业动态 更新时间:2024-10-27 22:25:42
本文介绍了无法将类型System.EventHandler隐式转换为System.EventHandler< object>.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在Windows Phone应用中实现计时器.它在Windows Phone应用(Silverlight)中可以正常运行,但在Windows Phone空白应用中不起作用.但是它给我以下错误-

I am trying to implement a Timer in windows phone app.It works fine in Windows Phone App(Silverlight) but it isnt working in Windows Phone Blank App.But it gives me following error-

Cannot implicitly convert type System.EventHandler to System.EventHandler<object>

这是我的代码-

namespace Timer { public partial class MainPage : Page { DispatcherTimer mytimer = new DispatcherTimer(); int currentcount = 0; public MainPage() { InitializeComponent(); mytimer = new DispatcherTimer(); mytimer.Interval = new TimeSpan(0, 0, 0, 1, 0); mytimer.Tick += new EventHandler(mytime_Tick); //HERE error comes Cannot implicitly convert type System.EventHandler to System.EventHandler<object> } private void mytime_Tick(object sender,EventArgs e) { timedisplayBlock.Text = currentcount++.ToString(); } private void startButton_Click(object sender, RoutedEventArgs e) { mytimer.Start(); } } }

我尝试了无法隐式转换类型' System.EventHandler"到"System.EventHandler< object>"情节提要完整

但这甚至有所帮助.如何解决此错误?

But it even helped.How can I fix this error?

推荐答案

直接引用事件的方法处理程序将推断类型,满足通用类型声明要求.

Referencing the method handler for the event directly will infer the type, satisfying the generic type declaration requirement.

mytimer.Tick += mytime_Tick;

或者,显式声明泛型并使用泛型EventHandler构造函数,

Alternatively, explicitly declaring the generic type and using the generic EventHandler constructor,

mytimer.Tick += new EventHandler<object>(mytime_Tick);

可以解决问题.

此外,根据文档,您的处理程序签名错误.应该是:

In addition, according to the documentation, your handler has the wrong signature. It should be:

private void mytime_Tick(object sender,object e) { timedisplayBlock.Text = currentcount++.ToString(); }

更多推荐

无法将类型System.EventHandler隐式转换为System.EventHandler&lt; object&gt;.错误

本文发布于:2023-11-07 06:58:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   错误   类型   隐式   EventHandler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!