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:

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.

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:

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.

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.

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.

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.

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.

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:

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.

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.