Hari Vignesh
Hari Vignesh

Reputation: 21

cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<object>'

I am trying to fade a text box within specified time. This code works on Windows Phone but it doesn't work in a Windows 8 application. I made changes to fix as many errors as possible. but I couldn't resolve one of them: cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<object>" arises on the expression sb.Completed += ehl. The full function is below:

    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();
    }

Upvotes: 2

Views: 5473

Answers (1)

sloth
sloth

Reputation: 101032

The type of the Completed event is EventHandler<object>, but you declare your event handler is of type EventHandler, and not EventHandler<object>.

That's exactly what the error message tells you, so you should change the type of your event handler to EventHandler<object>.

BTW, the line sb.Completed+= ehl; inside the event handler seems strange to be. You are basically attaching the handler to the event again every time the event handler is called.

Upvotes: 2

Related Questions