basquang™ on clouds

September 26, 2013

Windows Store RT: Save game/application state when closing app via close gesture or Alt+F4

Filed under: Windows Store 8 — basquang @ 2:41 PM
Tags: , , ,

If you has already family with Windows Phone development. You may often saving application/game state by overriding MainPage’s OnNavigatedFrom event. But with Windows Store app (Windows RT). The event is not fire when user is closing application by “close gesture” or Alt+F4 keyboard function.

So the question is how to save application/game state in that case?

According to MSDN “When the user closes an app by pressing Alt+F4 or using the close gesture, the app is suspended for 10 seconds and then terminated. Windows notifies your app when it suspends it, but doesn’t provide additional notification when it terminates the app. That means your app must handle the suspended event and use it to save its state and release its exclusive resources and file handles immediately.”

Meaning that you should implement your code in the suspending event. So the process of saving application/game state when use close app via close gesture/Alt+F4 is:

  1. Handle Suspending event in the App.xaml.cs constructor. In the Suspending event, saving the application/game state to Application Data (ex: Windows.Storage.ApplicationData.Current.LocalFolder)
    public App()
            {
                this.InitializeComponent();
                this.Suspending += OnSuspending;
            }
    
    private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
    
                //TODO: Save application state and stop any background activity
                await SuspensionManager.SaveAsync();
    
                await SaveGameState();
                await UpdateTile();
    
                deferral.Complete();
            }
    
     async System.Threading.Tasks.Task SaveGameState()
            {
            //Save application or game state to Windows.Storage.ApplicationData.Current.LocalFolder here
            }
    
  2. In the MainPage.xaml.cs load the state from Application Data in the OnNavigatedTo event.
     protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
                //Load application/game state from Windows.Storage.ApplicationData.Current.LocalFolder here
                await LoadGameState();
            }
    
Advertisements

4 Comments »

  1. average income

    Windows Store RT: Save game/application state when closing app via close gesture or Alt+F4 | basquang™ on clouds

    Trackback by average income — September 25, 2014 @ 1:37 PM | Reply

  2. Excellent way of explaining, and good piece of writing to get information on the topic of my presentation subject,
    which i am going to convey in college.

    Comment by Ahmed — October 3, 2014 @ 3:21 PM | Reply

  3. Wonderful goods from you, man. I’ve be aware your stuff previous to and you
    are just extremely excellent. I actually like what you have
    acquired here, really like what you’re stating and
    the way during which you are saying it. You make it entertaining and you continue to care for
    to stay it sensible. I can’t wait to read far more from you.

    That is really a great site.

    Comment by Sherryl — October 4, 2014 @ 4:15 PM | Reply

  4. You really make it seem so easy with your presentation but
    I find this topic to be really something which I think I
    would never understand. It seems too complicated and extremely broad for me.

    I am looking forward for your next post, I will
    try to get the hang of it!

    Comment by Madden NFL Mobile Hack — October 5, 2014 @ 2:41 PM | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: