wp7appdevelopment

Adventures in Windows Phone app development

Monthly Archives: March 2014

Windows Phone: WrapPanel

The WrapPanel control is available for download as part of the Windows Phone Toolkit (Microsoft.Phone.Controls.Toolkit). Similar to a ListBox, this control positions multiple child elements. Like a ListBox, it positions child elements sequentially — either left to right (horizontally), or top to bottom (vertically). However, with a WrapPanel, when the elements extend beyond the panel’s edge, they are then positioned in the next row or column.

Here are some good resources for learning more about WrapPanel: 

 

 

Tombstoning in Windows Phone 8 Apps

I have a simple Windows Phone game, and my way of dealing with tombstoning is to just start a new game. To do this, I need to first detect that that app was tombstoned. I check for this every time I navigate to a page. If tombstoning has occurred, I navigate to the main page of my app/game and delete the navigation back stack, effectively starting fresh.

NOTE: Because my game is so simple, I don’t need to bother with saving the game state or any data. Most apps will want to save info instead of just starting a new game.

[1] Create a entry in the isolated storage to store a boolean value of whether the app has been tombstoned.

I use a helper class, AppSettings, to store and retrieve values from Isolated Store. See this post for more info. The default value is false.

const string WasTombstonedKeyName = "WasTombstoned";
const bool WasTombstonedDefault = false;
 public bool wasTombstoned
        {
            get
            {
                return GetValueOrDefault<bool>(WasTombstonedKeyName, WasTombstonedDefault);
            }
            set
            {
                AddOrUpdateValue(WasTombstonedKeyName, value);
                Save();
            }
        }

[2] You can check if an app has been activated in the Application_Activated() method of App class (App.xaml.cs). This method is executed when an application is activated (brought to the foreground). It does not execute when the application is first launched.

Add the following code to set the value of our WasTombstoned flag in the (isolated store) to either true or false:

 private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            AppSettings mySettings = new AppSettings();

            if (e.IsApplicationInstancePreserved)
                mySettings.WasTombstoned = false;
            else
                mySettings.WasTombstoned = true;
        }

[3] Modify the OnNavigatedTo method of your MainPage.xaml.cs. You’ll want to clear the WasTombstoned flag, and also clear the navigation backstack.

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Remove entries from the backstack, so that a back key press exits the app.
            try
            {
                while (NavigationService.CanGoBack)
                    NavigationService.RemoveBackEntry();
            }
            catch
            {
            }

            // clear the wasTombstoned flag
            mySettings.wasTombstoned = false;

            base.OnNavigatedTo(e);
        }

[4] On all other pages, edit the OnNavigatedTo method to check for Tombstoning and return to the MainPage if it occurred.

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            AppSettings myAppSettings = new AppSettings();

            base.OnNavigatedTo(e);

            if (myAppSettings.wasTombstoned)
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

[5] Testing
Assuming you are using Visual Studio to build your app, the easiest way to test tombstoning is to edit the Properties:

  1. In the Solution Explorer, double-click on Properties to open that panel.
  2. Left-click on the Debug tab (left side of panel).
  3. Click the checkbox for “Tombstone upon deactivation while debugging.”

Now, when you are testing, when you leave the application (for example, when you press the windows key or search key on the bottom of the phone) and then return to the application (for example, by pressing the back key), the application will have been tombstoned.