locked
calling method in another class RRS feed

  • Question

  • I have two windows open (A menuscreen and another window, separate classes). Both windows are always open the one not needed is set faded out and sent to the background.

    When I reclick the link on the menuscreen I want to fade back in the other window. This would be easy if I can find some way of calling a method on the other screen, from the menuscreen, but can't. I had some luck by setting the method as static but caused more problems.

    My menuscreen has a class of MenuScreen and the other window has a class of Playlist. I was able to do all of this using windows forms but am trying to move to WPF. All the windows/classes are in the same namespace so would have thought that I could call the method by using:

    Namespace.ClassIWantToCall.MethodIwantToCall()

    Or that type of thing.

    Please can anyone help?

    Wednesday, May 26, 2010 7:36 PM

Answers

  • If it's a Window, you can simply add a public property on your class which derives from Window, find it using Application.Current.Windows (or Application.Current.MainWindow), cast it to the your custom Window type, then directly call the method.

    If instead your custom class is a child of Window, you can simply use the FindName() API on its containing window to find it by its name (specified with x:Name="foo", or just Name="foo"), cast this to the class that defines the method you want to call, and call it.

    Hope this helps,
    Matt


    SDET : Deployment/Hosting
    • Marked as answer by Linda Liu Friday, May 28, 2010 6:42 AM
    Wednesday, May 26, 2010 8:37 PM
  • Think i got it, used something like:

    ((Playlist)App.Current.Windows[1]).playlistFadeIn(sender,e);

    and it worked.

    will defo need to read more.

    thanks

    • Marked as answer by Linda Liu Friday, May 28, 2010 6:42 AM
    Wednesday, May 26, 2010 9:05 PM

All replies

  • If it's a Window, you can simply add a public property on your class which derives from Window, find it using Application.Current.Windows (or Application.Current.MainWindow), cast it to the your custom Window type, then directly call the method.

    If instead your custom class is a child of Window, you can simply use the FindName() API on its containing window to find it by its name (specified with x:Name="foo", or just Name="foo"), cast this to the class that defines the method you want to call, and call it.

    Hope this helps,
    Matt


    SDET : Deployment/Hosting
    • Marked as answer by Linda Liu Friday, May 28, 2010 6:42 AM
    Wednesday, May 26, 2010 8:37 PM
  • Thanks matt, I think I get what you mean but don't know how to implement it.

    I'm quite new to WPF etc so not sure what you mean by creating public property etc. Sorry I really am a noob to all this.

    I would be so grateful if you could provide an example or more explanation. The first method seems the appropriate one if that helps.

    Thanks

    Wednesday, May 26, 2010 9:00 PM
  • Think i got it, used something like:

    ((Playlist)App.Current.Windows[1]).playlistFadeIn(sender,e);

    and it worked.

    will defo need to read more.

    thanks

    • Marked as answer by Linda Liu Friday, May 28, 2010 6:42 AM
    Wednesday, May 26, 2010 9:05 PM