不能隐式转换类型&System; Event.EventHandler&#39;到&#39; System.EventHandler <object>&#39; </object>

时间:2013-03-28 06:26:58

标签: c# windows-8

我正在尝试在指定时间内淡化文本框。此代码适用于Windows Phone,但在Windows 8应用程序中不起作用。我做了更改以尽可能多地修复错误。但是我无法解决其中一个:cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<object>"出现在sb.Completed += ehl这个词上。完整功能如下:

    public static void Fade(UIElement target, double ValueFrom, double ValueTo, double Duration)
    {
        DoubleAnimation da = new DoubleAnimation();
        da.From = ValueFrom;
        da.To = ValueTo;
        da.Duration = TimeSpan.FromSeconds(Duration);
        da.AutoReverse = false;

        Windows.UI.Xaml.Media.Animation.Storyboard.SetTargetProperty(da, "opacity");

        Windows.UI.Xaml.Media.Animation.Storyboard.SetTarget(da, target);

        Windows.UI.Xaml.Media.Animation.Storyboard sb = new Windows.UI.Xaml.Media.Animation.Storyboard();
        sb.Children.Add(da);

        EventHandler ehl = null;
        ehl = (s, args) =>
        {

            sb.Stop();
            sb.Completed+= ehl; //error occurs here
            target.Opacity = ValueTo;
        };
        sb.Completed += ehl; //error occurs here

        sb.Begin();
    }

1 个答案:

答案 0 :(得分:2)

Completed event的类型为EventHandler<object>,但您声明事件处理程序的类型为EventHandler,而不是EventHandler<object>

这正是错误消息告诉您的内容,因此您应该将事件处理程序的类型更改为EventHandler<object>

顺便说一下,事件处理程序里面的行sb.Completed+= ehl; 似乎很奇怪。每次调用事件处理程序时,您基本上都会将处理程序附加到事件