Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #72 - How to call Page.xaml.cs from App.xaml.cs

Your App class (App.xaml + App.xaml.cs) is primarily used for global events and declarations such as styles.

The primary events this class handles by default include:

  1. Application_Startup() - called when the applications starts.
  2. Application_Exit() - called when the application is exiting.
  3. Application_UnhandledException() - called when an unhandled exception has occurred.

Your Page class (Page.xaml + Page.xaml.cs) is where you will typically put the core application logic and UI declarations. Now, what if you want to communicate with your game logic in your Page class when an event such as Application_Exit() is fired in App class?

In Application_Startup() our App class creates an instance of your page class and stores it as a UIElement under the property RootVisual:

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new Page();
}

To illustrate the call let's add a public getter function in Page.xaml.cs:

public partial class Page : UserControl
{
    private static bool _done = false;
 
    public bool Done
    {
        get { return _done; }
        set { _done = value; }
    }
}

In order to call any public members in the page class you will have to typecast RootVisual to be a Page since by default it is a UIElement. In App.xaml.cs let's make a call to set done = true in the Page class when the application has exited:

private void Application_Exit(object sender, EventArgs e)
{
    ((Page)this.RootVisual).Done = true;            
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Your App class (App.xaml + App.xaml.cs) is primarily used for global events and declarations such as

# November 25, 2008 3:22 PM

chrisaswain said:

Another option is to subscribe to the App.Current.Exit event from within your page or control.  

# November 25, 2008 4:57 PM

mike.snow said:

Ah good point. I had looked for this.Exit not finding the event like you get for loading this.Loaded. I'll add a tip on it.

# November 25, 2008 5:13 PM

Tip of the Day - Pages, Threading und Monitoring at Programming with Silverlight, WPF & .NET said:

Pingback from  Tip of the Day - Pages, Threading und Monitoring at Programming with Silverlight, WPF & .NET

# November 26, 2008 3:33 AM

Tip of the Day - Pages, Threading und Monitoring at Programming with Silverlight, WPF & .NET said:

Pingback from  Tip of the Day - Pages, Threading und Monitoring at Programming with Silverlight, WPF & .NET

# November 26, 2008 3:33 AM

2008 November 26 - Links for today « My (almost) Daily Links said:

Pingback from  2008 November 26 - Links for today « My (almost) Daily Links

# November 26, 2008 4:03 AM

Dew Drop - November 26, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - November 26, 2008 | Alvin Ashcraft's Morning Dew

# November 26, 2008 11:25 AM

Community Blogs said:

Happy Thanksgiving everyone! In this issue: Martin Mihaylov, Tim Heuer, Katrien De Graeve, Expression

# November 27, 2008 2:33 PM

cs | HP.com HP United States said:

Pingback from  cs | HP.com HP United States

# November 27, 2008 10:50 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

Visual Web Developer Team Blog said:

This link provides a complete Tips of the Day Summary Outline - silverlight.net/.../silverlight-tips-of-the-day-summary-outline.aspx

# January 8, 2009 6:37 PM

jin_u as blog » Blog Archive » ??????????????? ??? ?????? said:

Pingback from  jin_u as blog  » Blog Archive   » ??????????????? ??? ??????

# January 14, 2009 8:58 PM

jackbond said:

Hi Mike,

I use a slightly more flexible approach; on my pages that need to be notified when the application is exiting, I have them implement the IApplicationExit interface. This way, in multiple page apps you don't need to figure out what type to cast to.

public interface IApplicationExit

{

    void ExitApplication();

}

private void Application_Exit(object sender, EventArgs e)

{

    IApplicationExit ie = RootVisual as IApplicationExit;

    if(ie != null)

    {

         ie.ExitApplication();

    }

}

# February 10, 2009 5:02 AM

mike.snow said:

jackbond- Ah, good idea! Thanks for sharing it.

# February 10, 2009 12:18 PM