This post is Day #23 in a series called the 31 Days of Windows Phone.
Yesterday, I wrote about how you could add your game to the Games hub on a Windows Phone. Today, I’m going to show you how simple it is to set up your application to have Trial sections. For example, let’s say you create a game that has 50 levels. Perhaps you want the user to be able to play the first 5 levels for free, but to play the rest, they need to buy the game. This article will show you how to do that.
Using the LicenseInformation Class
By adding the Microsoft.Phone.Marketplace assembly to our page, we have access to the LicenseInformation class, which is directly related to the “paid” status of our application.
using Microsoft.Phone.Marketplace;
Our next step is to actually use the LicenseInformation class, so let’s create an instance of it:
LicenseInformation li = new LicenseInformation();
Finally, LicenseInformation has a very nice Boolean property called IsTrial(), not surprisingly, that allows us to check if we’re in a Trial state or not. You can use it in a simple if statement, like this:
if (!li.IsTrial()) { //Do something that only paid users can do. } else { //Do something that all users, trial or paid, can do. }
Testing Trial Mode
Unfortunately, there’s not a built-in mechanism to just swap between Trial and Paid states. How I am handling this is pretty simple. I’m using the same IF statement that you find in your App.xaml.cs file. It checks to see if you’re debugging, and if you are, sets an IsolatedStorageSetting that I call “trialMode”.
Here’s the entire App() method, including the default code that is in our App.xaml.cs file. In my example below, I am setting my trialMode variable to TRUE. Turn this off when you want to test “paid” mode.
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; public App() { // Global handler for uncaught exceptions. UnhandledException += Application_UnhandledException; settings["trialMode"] = false; // Show graphics profiling information while debugging. if (System.Diagnostics.Debugger.IsAttached) { settings["trialMode"] = true; // Display the current frame rate counters. Application.Current.Host.Settings.EnableFrameRateCounter = true; // Show the areas of the app that are being redrawn in each frame. //Application.Current.Host.Settings.EnableRedrawRegions = true; // Enable non-production analysis visualization mode, // which shows areas of a page that are being GPU accelerated with a colored overlay. //Application.Current.Host.Settings.EnableCacheVisualization = true; } // Standard Silverlight initialization InitializeComponent(); // Phone-specific initialization InitializePhoneApplication(); }
Revisiting my code from earlier, I need to modify my IF statement to handle this new IsolatedStorageSettings value. I am including my entire MainPage.xaml.cs file this time, so you can see everything in context.
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.Marketplace; using System.IO.IsolatedStorage; namespace Day23_UsingTrial { public partial class MainPage : PhoneApplicationPage { LicenseInformation li = new LicenseInformation(); IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; // Constructor public MainPage() { InitializeComponent(); if (!li.IsTrial()||(bool)settings["trialMode"] == false) { //Do something that only paid users can do. } else if (li.IsTrial() || (bool)settings["trialMode"] == true) { //Do something that all users, trial or paid, can do. } } } }
That’s about all you need to do, though, and this is not necessarily the “best” way to handle this situation, but it’s been working for me. If someone has a better way, I’d love to start using that instead.
Download the Code
To see all of this in a working example, download this solution file and play with it. That’s always the best way to learn how stuff works.
Leave a Reply