views:

26

answers:

1

Hi everybody, I'm a rookie in c# and I'm working on a player with an specialized card. I've work on a usercontrol with the typical buttons stop play, pause and record. The problem is that I don't know how to create the events such that from the principal code (Window) I could manage and call the correspondent function, since from the usercontrol file I can't acces any of the items (of course defined in window) I need to make the things work, as they are not accesible.

Thanks in advance

Best regards,

Oscar

A: 

Your UserControl needs to expose appropriate properties, methods and events, so that your Window can interact with it. For example, your UserControl might declare a Paused event, so that the Window could respond when the user pauses the control; or it might declare a Play method, so that the Window can start the control playing. When you instantiate your UserControl in the Window's XAML file, give it a name, e.g.

<local:MyControl x:Name="myControl" />

You can then refer to it from code-behind, e.g.:

myControl.Paused += MyControl_Paused;
myControl.Play();

To create the required API, create public properties, methods and events on the UserControl in the code-behind class. You say you are a "rookie in C#" so I don't know how much guidance you need, but unless you need to get into WPF data binding or event routing, you can use normal C# / .NET properties, methods and events for this. Declaring properties, methods and events in C# is widely covered in MSDN and the literature. If you specifically need help working with WPF data binding or event routing, leave a comment outlining your specific difficulty and I'll update the answer.

itowlson
Thank you for your response, but I think that my problem is bigger because my play and pause methods can only be implemented in the window object and the necessary methods and objects are not accesible from my usercontrol... what ca I do?Best regardsOscar
Oscar
Where are the "necessary methods and objects" accessible from? *Something* must contain them: have your Window talk to that. For example, have your user control raise a `PlayPressed` event; the Window can receive that and call the `specialisedCard.Play()` method.
itowlson