无法将类型System.EventHandler隐式转换为System.EventHandler< object>.错误 [英] Cannot implicitly convert type System.EventHandler to System.EventHandler<object> error

查看:469
本文介绍了无法将类型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;.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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