Question

I am asking for this because when i do a switchable application bars in pivot page (four page) i have to hard code the handlers in app.xaml.cs? or is there a way to call the handlers from App.xaml.cs inside ?

Était-ce utile?

La solution

It is possible, for example you can make your eventhandler public and static:

public static void StaticButton_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Event from App.xaml.cs!");
}
// ten at any Page you can subscribe like this:
myButton.Click += App.StaticButton_Click;

You can also get to your eventhandler by App instance like this (just make your handler public):

public void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Event from App.xaml.cs!");
}
// then you can subscribe like this:
myButton.Click += (App.Current as App).Button_Click;

But you must be aware that those methods have limitations - you can't easily get to your Page's elements in this handler - it's universal so you would have to pass some information to it from which Page it was invoked and so on (for example you can use some other handlers). But maybe as it is designed as universal so maybe there will be no need for such a thing - everything depends on your App.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top