13 out of 14 rated this helpful - Rate this topic

How to: Implement a Trial Experience in a Silverlight Application for Windows Phone

Windows Phone

March 22, 2012

This topic describes how to implement a trial experience in your Windows Phone application. The goal of a trial experience is to give the user an opportunity to try your application before they commit to buying it. A trial experience typically limits the application, for example, by disabling features, by disabling levels in a game, or by setting a time limit on the usage of the application. These are just examples of the kind of limitations you can implement for your application. The limitation you choose for your application is entirely up to you. In this topic, a simple application will be created that has a feature limit in trial mode. That is, some features are disabled when the application is running in trial mode. Determination of the mode in which the application is running is done using the LicenseInformation class. For information about implementing a trial experience for an XNA Framework application, see How to: Implement a Trial Experience in an XNA Framework Application for Windows Phone. A completed sample of the functionality described in this application is available as a download. For more information, see Trial Applications Sample (Silverlight).

This topic contains the following sections:

In this section, you create the UI for demonstrating an application with a trial experience. This is a Silverlight for Windows Phone application. When the application starts, it checks the current license for the application. If the application has a trial license, it disables a feature. If the application has a full license, it enables all features. Real license information is available for an application only after it has been published to the Windows Phone Marketplace. Therefore, to test the trial experience, we must simulate license detection. This will be done when the application is running in Debug mode by programmatically setting an IsTrial property in the application. After the application is published, the IsTrial property in the application will be set by examining the LicenseInformation.IsTrial property.

To create the application user interface

  1. In Visual Studio 2010 Express for Windows Phone, create a new project by selecting the File | New Project menu command.

  2. The New Project window is displayed. Expand the Visual C# templates, and then select the Silverlight for Windows Phone templates.

  3. Select the Windows Phone Application template. Fill in the Name with a name of your choice.

  4. Click OK. A new project is created, and MainPage.xaml is opened in the Visual Studio designer window.

  5. On MainPage.xaml, remove the XAML code for the Grid named “LayoutRoot” and replace it with the following code.

    
            <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <!--TitlePanel contains the name of the application and page title.-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="Trial Application Experience" Style="{StaticResource PhoneTextNormalStyle}" Margin="5"/>
                <TextBlock x:Name="PageTitle" Text="main page" Style="{StaticResource PhoneTextTitle1Style}" Margin="5"/>
            </StackPanel>
    
            <!--ContentPanel - place additional content here-->
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal"
                            HorizontalAlignment="Center" VerticalAlignment="Center">
                    <TextBlock Text="License:" FontSize="{StaticResource PhoneFontSizeExtraLarge}" />
                    <TextBlock x:Name="tbTrialStatus" FontSize="{StaticResource PhoneFontSizeExtraLarge}" />
                </StackPanel>
                <TextBlock Grid.Row="1" Text="My Application Features" HorizontalAlignment="Center"/>
                <StackPanel Grid.Row="2"  Orientation="Horizontal" HorizontalAlignment="Center">
                    <Button Content=" A" Height="78" Width="100" x:Name="btnFeatureA" Click="btnFeatureA_Click"/>
                    <Button Content=" B" Height="78" Width="100" x:Name="btnFeatureB" Click="btnFeatureB_Click"/>
                    <Button Content=" C" Height="78" Width="100" x:Name="btnFeatureC" Click="btnFeatureC_Click"/>
                </StackPanel>
                <Button Grid.Row="3" x:Name="btnBuyApplication" Content="Buy the Application" Click="btnBuyApplication_Click" Height="78"/>
            </Grid>
        </Grid>
    
    

    The preceding XAML code creates a simple user interface that looks like the following screenshot.

    Screenshot for Silverlight Trial Application

    The application contains three buttons, each representing a feature of the application. The UI also contains status text showing the current license information and a button to buy the application. The preceding code and screenshot serve as a basic example to illustrate the core concepts of building a trial experience. The event handlers for each of the buttons will be defined in the following sections.

In this section, you use the LicenseInformation object to determine the current license of the application. Real license information is available for an application only after it has been published to the Windows Phone Marketplace. Therefore, to test the trial experience, we must simulate license detection. This will be done when the application is running in Debug mode by programmatically setting an IsTrial property in the application. After the application is published, the IsTrial property in the application will be set by examining the LicenseInformation.IsTrial property.

To check for a trial license in your application

  1. Open App.xaml.cs and add the following using directive to the top of the page.

    
    using Microsoft.Phone.Marketplace;
    
    
  2. In the App class, declare the following local static variable. The LicenseInformation class enables an application to determine if it is running under a trial license.

    
         private static LicenseInformation _licenseInfo = new LicenseInformation();
    
    
  3. In the same App class in App.xaml.cs, add the following read-only property and local variable. This property is used to cache the license information while the application is running. The application uses the property whenever the current license information needs to be checked.

    
            private static bool _isTrial = true;
            public bool IsTrial
            {
                get
                {
                    return _isTrial;
                }
            }
    
    
  4. In the following sections, the license information will be checked in the Application_Launching and Application_Activated event handlers. Since this logic is used in two locations, we wrap it for convenience into its own method. In the App class located in App.xaml.cs, add the following method.

    
            /// <summary>
            /// Check the current license information for this application
            /// </summary>
            private void CheckLicense()
            {
                // When debugging, we want to simulate a trial mode experience. The following conditional allows us to set the _isTrial 
                // property to simulate trial mode being on or off. 
    #if DEBUG
                string message = "This sample demonstrates the implementation of a trial mode in an application." +
                                   "Press 'OK' to simulate trial mode. Press 'Cancel' to run the application in normal mode.";
                if (MessageBox.Show(message, "Debug Trial",
                     MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    _isTrial = true;
                }
                else
                {
                    _isTrial = false;
                }
    #else
                _isTrial = _licenseInfo.IsTrial();
    #endif
            }
    
    

    The preceding method consists of two code paths, depending on whether the application is run in Debug mode or not. In Debug mode, the code between the conditional compilation directive #if Debug and the #else directive is executed. The user is asked whether they would like to simulate trial mode. If the user selects OK, then the _isTrial variable is set to true; otherwise, it is set to false. If the application is built in release mode, the code between the #else and the #endif is executed and the _isTrial variable is set by examining the IsTrial property on the LicenseInformation object.

  5. The license for the application should be checked whenever the application starts or resumes. This is done by checking the license information in the Application_Launching and Application_Activated event handlers. Replace the Application_Launching event handler in App.xaml.cs with the following code.

    
            private void Application_Launching(object sender, LaunchingEventArgs e)
            {
                CheckLicense();
            }
    
    
  6. Replace the Application_Activated event handler in App.xaml.cs with the following code.

    
            private void Application_Activated(object sender, ActivatedEventArgs e)
            {
                CheckLicense();
            }
    
    

In this section, you use the information about the application’s current license to limit the application in trial mode. How the application is limited is your choice. In this example, the application is limited in trial mode by disabling a button on the UI that represents a feature of the application. A button is also made visible on the UI to enable the user to buy the application through the Windows Phone Marketplace, by calling the Show method. Because this application has not been published to the Windows Phone Marketplace, the call to Show will result in an error. This is expected behavior.

To enable or disable a feature based on the current application license information.

  • In the MainPage.xaml.cs code-behind file, add the following methods.

    
           protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
    
                if ((Application.Current as App).IsTrial)
                {
                    btnBuyApplication.Visibility = System.Windows.Visibility.Visible;
                    btnFeatureC.IsEnabled = false;
                    tbTrialStatus.Text = "Trial";
                }
                else
                {
                    btnBuyApplication.Visibility = System.Windows.Visibility.Collapsed;
                    btnFeatureC.IsEnabled = true;
                    tbTrialStatus.Text = "Full";
                }
            }
    
            private void btnFeatureA_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("Feature A");
            }
    
            private void btnFeatureB_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("Feature B");
            }
    
            private void btnFeatureC_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("Feature C");
            }
    
    

    In the preceding code, the OnNavigatedTo method is overridden to determine the current license information whenever the user navigates to this page. The IsTrial property of the App class, defined in the previous sections, is examined to determine whether the application is running in trial mode or running with a full license. If this property returns true, the application is running in trial mode. The btnFeatureC button is disabled to simulate disabling a feature when in trial mode. The btnBuyApplication button is also made visible so that the user has a way to initiate an application purchase. Finally, the license status is set on the tbTrialStatus TextBlock.

    In the case where the IsTrial property is false, all features are enabled. The btnBuyApplication button is hidden, since the user has a full license of the application. Finally, the license status is set on the tbTrialStatus TextBlock. The three click events are used to show a message when the user clicks one of the feature buttons in the UI.

In this section, you use the MarketplaceDetailTask to bring the user to your application in the Windows Phone Marketplace so that the user can buy the application.

To prompt the user to buy the application

  1. Open MainPage.xaml.cs and add the following using directive to the top of the page.

    using Microsoft.Phone.Tasks;
    
    
  2. At the top of the MainPage class in the MainPage.xaml.cs code-behind file, add the following variable.

    
            // Declare the MarketplaceDetailTask object with page scope
            // so we can access it from event handlers.
            MarketplaceDetailTask _marketPlaceDetailTask = new MarketplaceDetailTask();
    
    
  3. In the MainPage.xaml.cs code-behind file, add the following method.

    
            private void btnBuyApplication_Click(object sender, RoutedEventArgs e)
            {
                _marketPlaceDetailTask.Show();
            }
    
    

    When the btnBuyApplication button is clicked on the UI, the preceding code will show the Windows Phone Marketplace client application and display the details page for the specified product. The user will be able to buy the application. In this example, the application is not published to the Windows Phone Marketplace. As a result, the _marketPlaceDetailTask.Show() call will result in an error being displayed. If this error has the error code 805a0941, then the call was successful and will operate correctly when the application is published. When your application is published, the Show will automatically detect your application’s unique ID and launch the correct details page in the Windows Phone Marketplace client application.

To run the application

  1. On a device or emulator, run the application by selecting the Debug | Start Debugging menu command.

    Note Note:

    The option to select trial mode simulation is available only when the application is run in debug mode. This is done to prevent trial mode simulation from running in the release build of the application and accidentally being published to the Windows Phone Marketplace with this test feature enabled.

  2. When the application starts, a message box is displayed. Click OK to simulate trial mode.

  3. In the main page of the application observe that the License field is set to Trial and the Feature C button is disabled.

  4. Click the Buy the Application button. An attempt is made to open the Windows Phone Marketplace client application.

  5. You will see an error dialog that looks like the following image. This indicates that the application successfully opened the Windows Phone Marketplace client application. On a published application, this error will not occur and you will be brought to the details page of the application on the Windows Phone Marketplace where you can purchase the application.

    MarketplaceDetailTask.Show() error
  6. Click Close on the error dialog. The application will resume.

  7. To run the application to simulate a full license, click Cancel on the MessageBox that appears in step two of this procedure. Alternatively, you can start the application by selecting the Debug | Start without Debugging menu command. In this mode, the Buy the Application button will be hidden and the Feature C button will be enabled.

Did you find this helpful?
(1500 characters remaining)