Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - Cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler<object>' for storyboard complete

I have read already a few threads about this, but I still don't know how to solve it in my case. I come from Java and mostly new to C#

I want to attach listener when animation finishes:

myStoryBoard.Completed += new EventHandler(onMyStoryBoardCompleted);

And:

private void onMyStoryBoardCompleted(object sender, EventArgs e)
{       
}

And I get the error in the title. I tried:

 myStoryBoard.Completed += new EventHandler<object>(onMyStoryBoardCompleted);

But then I get:

no overload for 'onMyStoryBoardCompleted' matches delegate 'System.EventHandler<object>'

So it seems that the signature is not compatible with EventHandler<object> and I couldn't find how to make it compatible, I also don't know if this approach is correct.

I read

Understanding events and event handlers in C#

C# Dynamic template implicit conversion error from System.EventHandler to System.EventHandler<TEventArgs>

defining event handler for Tick event of DispatcherTimer in windows 8 app

But still don't find the solution for this case.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try:

private void onMyStoryBoardCompleted(object sender, object e)
{ }

And subscribe using the generic EventHandler<object>:

myStoryBoard.Completed += new EventHandler<object>(onMyStoryBoardCompleted);

Of course, this goes against the .NET Framework convention that the second argument to an event handler should be an instance of EventArgs (or a class derived thereof). I am assuming that you are running on another framework, such as Windows 8 Metro, whose Timeline class defines a Completed event with an EventHandler<object> signature.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...