(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Windows Phone 7 – Creating Trial Applications

(11 votes)
Pencho Popadiyn
>
Pencho Popadiyn
Joined Apr 30, 2008
Articles:   22
Comments:   97
More Articles
5 comments   /   posted on Apr 06, 2011
Categories:   Windows Phone

In this short article, I would like to show you how to use the WP7 Trial API in your applications. As you know, there are three types of applications in the WP7 marketplace – free, buy and try-before-buy. It is a good practice to offer the user to try the application before buying it.

The WP7 Trial API is an extremely powerful and in the same time simple tool. Probably, the first thing that crosses your mind is that you need to produce two xap files – one that works in trial mode, and second one that works in full mode. Well, that’s not true; you don’t have to create two applications. All hard work is handled by Microsoft. When you publish your application in the marketplace, you have the opportunity to select whether to include trial mode or not. If you enable the trial mode, the Marketplace license associated with your application will have its IsTrial flag set to true. Thus the user can try your application without purchasing it, and eventually buy it at later stage. When the application is purchased, the IsTrial flag will be reset to false.

Integrating the Trial API in your application is a straightforward process. You need to use the Microsoft.Phone.Marketplace.LicenseInformation class in the following manner:

LicenseInformation license = new LicenseInformation();
bool isInTrialMode = license.IsTrial();

How it works? The LicenseInformation class checks the application license for the application. It returns true if the license is a trial version or false if it is the full version. The way you implement your logic and organize the user interface is completely up to you. For example, you could limit the available functionality within the trial mode. One common scenario for desktop applications is to enable all the functionality but limit the Trial to operate for a fixed period of time. After the time expires all functions are disabled. I think this is not too much applicable for windows phone 7 applications. The reason is that you have to store somewhere the time the application has been on the device. You could do that by using the isolated storage. However, once the user uninstalls the application, all information is deleted from the isolated storage. Another solution is to use some web services to send information to a central database, but this will increase the complexity of your application. So it is up to you.

Debugging a Trial Application on the WP7 Emulator

During development the IsTrial flag is always set to false. That makes it difficult to test the functionality and the user interface when the application runs in trial mode. I know that when it comes to workarounds we are creative :). My favorite workaround is the following: first, you could wrap the LicenseInformation class.

using Microsoft.Phone.Marketplace;
namespace SLWP7TrialDemo
{
    public class ApplicationLicense
    {
        public bool IsInTrialMode
        {
            get
            {
                LicenseInformation license = new LicenseInformation();
                return license.IsTrial();
            }
        }
    }
}

The next step is to create a custom build configuration. It is very simple – open the Configuration Manager in Visual Studio.

Open the Active solution configuration drop-down and select <New…>. This will open the New Solution Configuration dialog. Here you need to specify a name for your configuration (for example “Trial”) and copy settings from the Debug configuration. Press OK to commit the changes.

Again in the Configuration Manager dialog, ensure that when the Active solution configuration is Trial, the Build Configuration for your project is set to Trial. Probably, that will be done automatically, but I would like to mention it.

Close the Configuration Manager dialog. We are half way there. The third step is to define a conditional compilation symbol for your project. Right-click on your project in Solution Explorer and select Properties. Navigate to the Build section and define a new compilation symbol (for example TRIAL) for the Trial configuration.

In case your solution contains more than one project, you don’t have to define the TRIAL symbol for each project. Just do it for the project that contains the LicenseInformation class. The final step is to modify the LicenseInformation class, which we created on step 1, so it uses the TRIAL conditional compilation symbol to determine whether the application calls the IsTrial method or simply assumes a constant value:

using Microsoft.Phone.Marketplace;
namespace SLWP7TrialDemo
{
    public class ApplicationLicense
    {
        public bool IsInTrialMode
        {
            get
            {
#if TRIAL
                return true;
#else
                LicenseInformation license = new LicenseInformation();
                return license.IsTrial();
#endif
            }
        }
    }
}

It is quite simple, when you want to debug the application in Trial mode, just change the build configuration to Trial. Respectively, if you want to debug your application in Full mode, then use the Debug configuration. And don’t forget to change the build profile to Release when your app is ready for deployment in the marketplace.

How about in XNA? Well there you have to query the Guide.IsTrialMode property. In order to simulate Trial mode, you can use the Guide.SimulateTrialMode property. The default value of the IsTrialMode is false.

Binding the ApplicationLicense.IsInTrialMode Property

If you need to display or hide visual elements based on whether the application is running in Trial mode or not, you could either integrate the ApplicationLicense class in your view model, or directly use it in XAML. Here is how it could look like in your base view model.

namespace SLWP7TrialDemo
{
    public class ViewModelBase
    {
        private static readonly ApplicationLicense applicationLicense = new ApplicationLicense();
 
        public bool IsInTrialMode
        {
            get
            {
                return applicationLicense.IsInTrialMode;
            }
        }
    }
}

Or you could create an instance of the wrapper class in the Resource Dictionary of the application in the App.xaml file:

<Application x:Class="SLWP7TrialDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
             xmlns:local="clr-namespace:SLWP7TrialDemo">
 
    <Application.Resources>
        <local:ApplicationLicense x:Key="LicenseInfo" />
    </Application.Resources>
 
</Application>

You could reference the resource from elsewhere in your XAML. Note that if you want to control the visibility you will need a BooleanToVisibilityConverter.

<StackPanel Visibility="{Binding IsInTrialMode, 
                Source={StaticResource LicenseInfo}, 
                Converter={StaticResource BooleanToVisibilityConverter}}"
            Grid.Row="1">
 
    <TextBlock Text="TRIAL VERSION" />
    <TextBlock Text="If you like my application, please buy it..."
                TextWrapping="Wrap" />
    <Button Content="Purchase"
            x:Name="PurchaseButton"
            Click="PurchaseButtonClick" />
 
</StackPanel>

The left image illustrates the application running in Full mode, while the right image illustrates the application running in Trial mode. A link for downloading the source code is provided at the end of the topic.

When your application is running in Trial mode, it would make sense to direct the user to the application page in the WP7 marketplace, so he/she could buy the application. You can achieve that via the MarketplaceDetailTask class:

MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
marketplaceDetailTask.Show();

Again, don’t forget that you have to use the Release build configuration, when it is time to publish your application. This doesn’t have the TRIAL compilation symbol declared, so the IsInTrialMode property will call the LicenseInformation class. You could download the source code for the demo from the link below. If you have any comments, feedback, questions, feel free to share them with us.

Download Source Code

About the author

An MCPD and MCTS, Pencho Popadiyn is one of SilverlightShow team members with most Silverlight experience. He is actively involved in the development of complex modules for SilverlightShow, and a great deal of his experience is shared through SilverlightShow articles (check out Pencho's posts). He is also programming WP7 in his free time. 

Pencho is a keen chess player, enjoys relaxation through jogging or nice music.


Subscribe

Comments

  • -_-

    RE: Windows Phone 7 – Creating Trial API Applications


    posted by alexis_cn on Apr 06, 2011 12:26
    thank you very much
  • Calabonga

    RE: Windows Phone 7 – Creating Trial Applications


    posted by Calabonga on Apr 08, 2011 07:54
    Thanks! This is very good information!
  • wp7agile

    RE: Windows Phone 7 – Creating Trial Applications


    posted by wp7agile on May 03, 2011 19:08
    Can you check how long an app has been running on Trial Mode? Suppose you want the user to get the full app after n-days of usage?
  • ppopadiyn

    RE: Windows Phone 7 – Creating Trial Applications


    posted by ppopadiyn on May 03, 2011 20:28

    @wp7agile

    You could do something like this. On first run of the application you initialize a new DateTime value and save it in the isolated storage. On each next run you load that value and compare it to DateTime.Today (and also check the IsInTrialMode variable). That is the most simple way for tracking the time the application has be installed on the user's phone. But when the user uninstall the application all data from the isolated storage related to that application will vanish. After he/she reinstall the app, everything start from the beginning. Another way is to use some kind of web services and extracting the phone identity somehow (I don't know how), and storing the information in a remote database.

  • wp7agile

    RE: Windows Phone 7 – Creating Trial Applications


    posted by wp7agile on May 04, 2011 14:05
    Good insights!

Add Comment

Login to comment:
  *      *