programming4us
programming4us
MOBILE

Windows Phone 7 Development : Understanding Trial and Full Modes (part 3) - Simulating Application Trial and Full Modes

2/19/2011 5:46:12 PM

3. Simulating Application Trial and Full Modes

While the ability to create both trial and full application versions in the same code base is a boon to the developers who must maintain them, the technique does complicate testing. To properly test the functionality of your application, you must be able to simulate trial and full application modes before you submit it to the Marketplace. There are certainly many ways to do this, from conditional compilation to use of the SimulateTrialMode property within the Microsoft.Xna.Framework.GamerServices namespace. Each approach has its merits; however, the best approach is often the one that requires the least amount of change to your code.

One way to test an application with virtually no change to its code is to implement your own fake version of the LicenseInformation class. As you've seen, the LicenseInformation class can implement the single IsTrial method. By implementing this class, you can fully control the value returned by the IsTrial method and thus the behavior of the application. Then, before going to production, you can swap your implementation of the LicenseInformation class for the sealed class provided by Microsoft within the Microsoft.Phone.Marketplace namespace.

In this section, you'll create such a class. In the next section, you will build an application that uses this class to test features available to trial and full versions of an application to ensure none of the premium content or features of the applications are leaked to trial users.

To help you create your own implementation of the LicenseInformation class, you can (but certainly don't have to) use Reflector.NET, a free tool available for download from Red Gate's web site, at www.red-gate.com/products/reflector/. Reflector is a great tool and becomes very handy when you would like to peek at the implementation details of common libraries. You can certainly gather a wealth of information not just about the LicenseInformation file, but also about the Microsoft.Phone assembly in general. Here's how to set up Reflector.NET to help you implement the example that follows:

  1. Download and run Reflector.NET, select File => Open, and navigate to the Microsoft.Phone.dll file located by default at C:\Program Files(x86)\ReferenceAssemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone.

  2. Drill down into Microsoft.Phone.dll => Microsoft.Phone.Marketplace => LicenseInformation, right-click the LicenseInformation class, and click Disassemble. Be sure to also click the Expand Methods link on the right.

You will see a screen similar to the one shown in Figure 3, which you will use to speed up your own implementation of the LicenseInformation class.

Now follow these steps to implement a version of the LicenseInformation class:

  1. Launch Visual Studio 2010 Express for Windows Phone if it is not already open, and open the Windows Phone Application project called "TrialSample," created during the previous walkthroughs.

  2. Right-click the project name in Solution Explorer, select Add => New Item, and then select Class from the list of available items. Name the new class "LicenseInformation" and click OK.

  3. Now, you can copy the LicenseInformation class definition and implementation from Reflector.NET and paste it into your application. Here's a copy of what you'll see.

    public sealed class LicenseInformation
    {
    // Fields
    private bool m_fIsTrial = true;

    // For testing purpose only!
    public bool IsTrial()
    {
    return m_fIsTrial;
    }
    // Nested Types
    internal static class NativeMethods
    {
    // Fields
    internal const int S_FALSE = 1;
    internal const int S_OK = 0;
    }
    }

    Figure 3. Using Reflector.NET to peek into the LicenseInformation class

    NOTE

    There's no reason not to simplify your own version of the class definition by implementing only the IsTrialmethod. There is no LicenseClass interface for you to follow.

  4. To eliminate any potential confusion between the fake version of the LicenseInformation class you use for testing your application and the real one provided by Microsoft, you will use the var keyword to create an instance of the LicenseInformation class and then use the fully qualified name to help you visually distinguish between your own implementation of LicenseInformation and the implementation provided by Microsoft. Open MainPage.xaml.cs and change the following line of code at the top of the page from

    LicenseInformation lic = new LicenseInformation();

    to

    var lic = new TrialSample.LicenseInformation();

Now, we can fully control return values of the IsTrial method by changing the value of the m_fIsTrial variable.

  1. Set the m_fIsTrialvariable to true and press F5 to run the application.

With these modifications, your TrialSample application will now prompt the user to upgrade to the full version. Changing the value of the m_fIsTrial variable to false and re-running the application results in a message that thanks the user for running an application with the full license.

Once you've verified that the application will behave correctly when you deploy it to the Marketplace, it's time to restore the official version of the LicenseInformation class. Here's how:

  1. To switch back to the LicenseInformation class within the Windows Phone 7 Framework, right-click LicenseInformation.cs in Solution Explorer and select "Exclude from Project." This action effectively excludes the file from the solution, but does not delete it from the computer system.

  2. Finally, we need to change the instance of LicenseInformation to be the instance of the LicenseInformation class provided by Microsoft. Open MainPage.xaml.cs and change the following line of code from

    var lic = new TrialSample.LicenseInformation();

    to

    var lic = new LicenseInformation();

Creating trial Windows Phone 7 applications is a relatively straightforward process, as you have seen so far. The biggest challenge is probably testing these applications to ensure that they behave as expected with both trial and full licenses. Luckily, you have several approaches at your disposal, which we've summarized and one of which we have described in detail.

In the next section, you will create trial and full versions of a more complete application and employ several other Windows Phone 7 development techniques that we cover elsewhere in this book. We hope that this short review will help you further solidify your knowledge of the Windows Phone 7 programming.

Other  
  •  Mobile Application Security : SymbianOS Security - Application Packaging
  •  Mobile Application Security : SymbianOS Security - Code Security
  •  iPhone Application Development : Getting the User’s Attention - Generating Alerts
  •  iPhone Application Development : Getting the User’s Attention - Exploring User Alert Methods
  •  iPhone Application Development : Using Advanced Interface Objects and Views - Using Scrolling Views
  •  Working with the Windows Phone 7 Application Life Cycle (part 2) - Managing Application State
  •  Working with the Windows Phone 7 Application Life Cycle (part 1) - Observing Application Life Cycle Events
  •  Mobile Application Security : SymbianOS Security - Development and Security Testing
  •  Mobile Application Security : SymbianOS Security - Introduction to the Platform
  •  Java Mobile Edition Security : Permissions and User Controls
  •  
    PS4 game trailer XBox One game trailer
    WiiU game trailer 3ds game trailer
    Top 10 Video Game
    -   Minecraft Mods - MAD PACK #10 'NETHER DOOM!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #9 'KING SLIME!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #2 'LAVA LOBBERS!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #3 'OBSIDIAN LONGSWORD!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Total War: Warhammer [PC] Demigryph Trailer
    -   Minecraft | MINIONS MOVIE MOD! (Despicable Me, Minions Movie)
    -   Minecraft | Crazy Craft 3.0 - Ep 3! "TITANS ATTACK"
    -   Minecraft | Crazy Craft 3.0 - Ep 2! "THIEVING FROM THE CRAZIES"
    -   Minecraft | MORPH HIDE AND SEEK - Minions Despicable Me Mod
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 92 "IS JOE DEAD?!"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 93 "JEDI STRIKE BACK"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 94 "TATOOINE PLANET DESTRUCTION"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 95 "TATOOINE CAPTIVES"
    -   Hitman [PS4/XOne/PC] Alpha Gameplay Trailer
    -   Satellite Reign [PC] Release Date Trailer
    Video
    programming4us
     
     
    programming4us