7

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.

Community
  • 1
  • 1
User
  • 31,811
  • 40
  • 131
  • 232
  • `myStoryBoard.Completed += new EventHandler(onMyStoryBoardCompleted);` should work – SLaks May 19 '13 at 16:27
  • Which StoryBoard class are you using? If it's `Timeline` derived, the `Completed` should be defined as type EventHandler, which means this should compile without error. – Peter Ritchie May 19 '13 at 16:30
  • I'm referencing a storyboard from XAML in UserControl.Resource tag – User May 19 '13 at 16:33
  • @SLaks: Not on Windows 8 Metro. Apparently, it really is `EventHandler`. – Douglas May 19 '13 at 16:43

1 Answers1

10

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.

Douglas
  • 53,759
  • 13
  • 140
  • 188
  • Did you try it using the generic `EventHandler`? What error did you get? – Douglas May 19 '13 at 16:38
  • Ah, that combination actually works! It's strange since the method signature was generated by Visual Studio. And yes, I'm developing for Metro. – User May 19 '13 at 16:47
  • Or well, not strange, Visual Studio did it correctly, since it generated it for EventHandler(onMyStoryBoardCompleted). – User May 19 '13 at 16:49
  • I was surprised too that Microsoft themselves would break the `EventArgs` convention. (I'm not particularly familiar with Metro.) – Douglas May 19 '13 at 16:49
  • 2
    @Murkaeus please don't add unnecessary comments, I think my profile doesn't look like I don't know how to use SO. – User May 19 '13 at 16:59
  • @Ixx all the more bewildering that you haven't marked the answer as correct – Murkaeus May 19 '13 at 17:02
  • 1
    @Murkaeus Is there a time limit or something to mark answers as correct? The fact that I added a comment saying "it works", doesn't mean necessarily that everything is fine, maybe I want to double check it (runtime, etc.). Maybe I have something else to do before that. Don't know how long you have been using this site, but FYI answer selection offen takes hours, days, or weeks. – User May 19 '13 at 17:37