2. Using the Marketplace APIs
Now that you know how to find
out whether an application is running in trial mode, let's add the
ability to review an application and buy it. To do that, you'll need two
new classes found in the Microsoft.Phone.Tasks namespace: MarketplaceDetailTask and MarketplaceReviewTask. These classes, known as application launchers on the Windows Phone 7 platform, contain all the necessary functionality for your application to switch from trial to full mode.
Both MarketplaceDetailTask and MarketplaceReviewTask classes implement a Show
method that launches the Windows Phone Marketplace application and
navigates to the specified application page. For the purposes of
allowing users to switch from trial license to full license, there is
little difference between these two classes. However, for reference
purposes, note that MarketplaceDetailTask allows an application developer to specify an ID of the application (in the ContentIdentifier property) to show the Windows Phone Marketplace page for. This application ID is optional—if it's not supplied to the MarketplaceDetailTask class, the details for the current application are shown in the Marketplace. The MarketplaceReviewTask class, on the other hand, does not expose any public properties, and its Show method displays the review page with an option to buy for the current application only.
Let's enhance the
TrialSample application created in the previous walkthrough with an
option to review and buy the application from the Windows Phone
Marketplace. We will enhance the code so that if an application is
executing in the trial mode, it will show two buttons—one with an option
to "Upgrade to Full Version" and another with an option to "Cancel" and
return to the main screen.
Follow these steps to
complete these enhancements. First, you'll add the new namespace
reference that contains the APIs you need, and then add the two new
buttons:
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 walkthrough.
Add the following statement to the top of the page:
using Microsoft.Phone.Tasks;
From the Toolbox, drag and drop a button onto the design surface directly beneath the textBlock1 control, as shown in Figure 2. With that button selected, press F4 to bring up the button properties window, change the button's Content property to "Upgrade to Full Version," and set the button's Visibility property to "Collapsed." Change the button's name to "btnUpgrade."
From the Toolbox, drag another button onto the design surface and drop it next to btnUpgrade, as shown in Figure 2. With that button selected, press F4 to bring up the button properties window and change the button's Content property to "Cancel." Change the button's Visibility property to "Collapsed." Change the button's name to "btnCancel." You should now end up with a design surface resembling Figure 2.
Next, you need to add code that responds to the new buttons.
Double-click the Upgrade to Full Version button and change the btnUpgrade_Click method to the following:
private void btnUpgrade_Click(object sender, RoutedEventArgs e)
{
MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
marketplaceReviewTask.Show();
}
In MainPage.xaml.cs, change the MainPage() constructor to the following:
public MainPage()
{
InitializeComponent();
LicenseInformation lic = new LicenseInformation();
if (lic.IsTrial() == true)
{
textBlock1.Text = "You are running a trial version of our software!";
btnUpgrade.Visibility = Visibility.Visible;
btnCancel.Visibility = Visibility.Visible;
}
else
{
textBlock1.Text = "Thank you for using the full version of our software!";
}
}
When you press F5 to run the
application now, there are two issues that immediately jump out at you.
First, the IsTrial method, in its current implementation within Windows
Phone 7 Framework, always returns false to the emulator. Because of
this, given the logic of the application's current implementation, you
will not see the buttons with options to upgrade to the full version of
an application. If you try to force those buttons to appear, by
incorrectly changing the if (lic.IsTrial() == true)if (lic.IsTrial() == false),
for example, then when you click the Upgrade to Full Version button, a
second issue surfaces. Since the application has not been approved and
is not even registered with Windows Phone Marketplace, you will get an
error (as expected) trying to display the application details page. statement to
For details on the Windows Phone Marketplace registration and application approval process.
Here, let's assume that once the proper application registration is in
place, Windows Phone Marketplace will properly display the application
details page using the MarketplaceReviewTask
class and then swap the trial license and a full application license if
the user decides to purchase the program. That still leaves us with the
need to properly test the application using both trial and full license
modes before submitting the application to the Marketplace to ensure
that all functionality that belongs to the full mode only is not
available to trial users. The process of simulating trial application
mode is the subject of the next section.