Creating a Windows Phone 7 Trial App: Advanced Buy Now Implementation

来源:互联网 发布:穿越火线手游高跳软件 编辑:程序博客网 时间:2024/05/12 22:48

Creating a Windows Phone 7 Trial App: Advanced Buy Now Implementation

published on: 5/18/2011 | Views: 12923 | Tags: Trial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Currently rated 5.00 by 6 people

by WindowsPhoneGeek

This is the last post from the "Creating a Windows Phone 7 Trial Application" series of three posts in which I explain all about creating a trial app in Windows Phone 7:

  • Creating a Windows Phone 7 Trial Application |Part1: Implementation and Best Practices
  • Creating a Windows Phone 7 Trial Application |Part2: Implementing Buy Now Functionality
  • Creating a Windows Phone 7 Trial Application |Part3: Advanced Buy Now Implementation

In this post I am going to talk about building advanced "Buy Now" functionality of a Windows Phone 7 trial app. I will demonstrate another approach with automatically showing/hiding messages depending on the calculated time since the Trial first launch date. As always all examples and source code are available at the end of the article.

  In short Trial mode gives you the option to allow users to try your application before buying it. The trial license does not expire, but is replaced by a full license when the customer purchases the application.

NOTE: Take a look at the previous articles from this series for reference:

  • Creating a Windows Phone 7 Trial Application |Part1: Implementation and Best Practices
  • Creating a Windows Phone 7 Trial Application |Part2: Implementing Buy Now Functionality

To begin with,  lets create a Windows Phone 7 application, configure the DebugTrial configuration and finally we will add the IsTrial static property (all this is described in details in the Part1 and Part2 of this series). The next step is to add "Buy Now" functionality.

Advanced "Buy Now" implementation - automatically show/hide message depending on the calculated time since the Trial first launch date

In this example we will show/hide a message(Popup) in Application_Launching and Application_Activated handlers depending on a particular time calculation.

NOTE: It is important to understand the whole execution mode, Application Lifecycle and Tombstoning of Windows Phone 7 apps! Take a look at Part1 of this article for reference.

We will also use IsolatedStorageSettings to store the FirstLauchDate value. (For more information and examples related to IsolatedStorage in WP7 take a look at our "All about WP7 IsolatedStorage" series of articles).

Here are the steps to implement this scenario:

Step1: At first we will create a new WP7 UserControl  and will name it BuyNowUserControl.xaml. We will add the following code into it:

91-13

XAML:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<GridBackground="Black">
    <Grid.RowDefinitions>
        <RowDefinitionHeight="90"/>
        <RowDefinitionHeight="*"/>
    </Grid.RowDefinitions>
    <BorderGrid.Row="0">
    <TextBlock  TextWrapping="Wrap"FontSize="32"Padding="10"
                   Text="Do not forget to buy the full version!" />
    </Border>
    <StackPanelGrid.Row="1"Orientation="Horizontal"HorizontalAlignment="Center">
        <Buttonx:Name="btnBuyNow"Content="Buy Now" Click="btnBuyNow_Click"/>
        <Buttonx:Name="btnContinueTrial"Content="Continue Trial" Click="btnContinueTrial_Click"/>
    </StackPanel>
</Grid>

We will use a dynamic popup to display a message to the user, so we will also implement ClosePopup() functionality in our UserControl:

C#:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
publicpartial class BuyNowUserControl : UserControl
{
    privatePopup popup = null;
 
    publicBuyNowUserControl()
        :this(null)
    {
    }
 
    publicBuyNowUserControl(Popup popup)
    {
        InitializeComponent();
        this.popup = popup;
    }
 
    privatevoid btnBuyNow_Click(objectsender, RoutedEventArgs e)
    {
        MarketplaceDetailTask marketplaceDetailTask = newMarketplaceDetailTask();
        marketplaceDetailTask.Show();
        this.ClosePopup();
    }
 
    privatevoid btnContinueTrial_Click(objectsender, RoutedEventArgs e)
    {
        this.ClosePopup();
    }
 
    privatevoid ClosePopup()
    {
        if(this.popup != null)
        {
            this.popup.IsOpen = false;
        }
    }
}

Step2: Add the following constants in App.xaml.cs. For the purpose of the example we will use 10 seconds as expiration period. In your case the value will be probably greater. So when running this sample have in mind that you will have to wait a little so that the trial expires and the message appears!

?
1
2
privatestatic TimeSpan TrialPeriodLength = TimeSpan.FromSeconds(10.0);
privateconst string FirstLauchDateKey = "FirstLaunchDate";

Step3: Next Go to App.xaml.cs. We will create there a CheckTrialPeriodExpired() which check/determine whether the trial period has expired:

  • 1. Use IsolatedstorageSettings so store the firstLauchDate
  • 2. If first launch date is available, calculate the time passed since then
  • 3. Then compare with the trial period length
  • 4. Subscribe to the Navigated event in order to show the popup  over the page after it has loaded
  • 5. if a value cannot be found for the first launch date // save the current date and time
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
privatevoid CheckTrialPeriodExpired()
{
    // when the application is activated
    // show message to buy the full version if trial period has expired
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    DateTime firstLauchDate;
    if(settings.TryGetValue<DateTime>(FirstLauchDateKey, outfirstLauchDate))
    {
        TimeSpan timeSinceFirstLauch = DateTime.UtcNow.Subtract(firstLauchDate);
        if(timeSinceFirstLauch > TrialPeriodLength)
        {
            // subscribe to the Navigated event in order to show the popup
            // over the page after it has loaded
            this.RootFrame.Navigated += newNavigatedEventHandler(RootFrame_Navigated);
        }
    }
    else
    {
        // if a value cannot be found for the first launch date
        // save the current date and time
        settings.Add(FirstLauchDateKey, DateTime.UtcNow);
        settings.Save();
    }
}

 

Step4: In App.xaml.cs go to RootFrame_Navigated and add the following code(Note we have subscribed to Navigated event in the previous Step3). Basically we will remove the Navigated event handler as it is no longer necessary and will create a new Popup that will show the message a the user. We will display the previously created BuyNowUserControlUserControl  (see Step1) in the popup.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
voidRootFrame_Navigated(objectsender, NavigationEventArgs e)
{
    // remove the Navigated event handler as it is no longer necessary
    this.RootFrame.Navigated -= newNavigatedEventHandler(RootFrame_Navigated);
 
    Popup popup = newPopup();
    BuyNowUserControl content = newBuyNowUserControl(popup);
    // set the width of the popup to the width of the screen
    content.Width = System.Windows.Application.Current.Host.Content.ActualWidth;
    popup.Child = content;
    popup.VerticalOffset = 300;
    popup.IsOpen = true;
}

NOTE: We use this Popup approach because you can not use a Popup declared in the Resources of App.xaml . Another approach could be to declare the popup programmatically with C# but this is definitely not the best solution. So the easiest was to have a popup and to be able to control it is to use a separate UserControl/Page/etc.

Step5: Next we will add CheckTrialState() method which will check the current Trial state of the app. At first we call DetermineIsTrial() to refresh the value of the IsTrial property, alert that do not execute further if app is in full version and finally call CheckTrialPeriodExpired() :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
privatevoid CheckTrialState()
{
    // refresh the value of the IsTrial property
    this.DetermineIsTrial();
 
    if(!IsTrial)
    {
        // do not execute further if app is full version
        return;
    }
 
    this.CheckTrialPeriodExpired();
}

Where DetermineIsTrial() is explained in Part1 of this series. In short here is the code :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Use static property for caching and easy access to the trial state
publicstatic bool IsTrial
{
    get;
    // setting the IsTrial property from outside is not allowed
    privateset;
}
 
privatevoid DetermineIsTrial()
{
#if TRIAL
    // return true if debugging with trial enabled (DebugTrial configuration is active)
    IsTrial = true;
#else
    var license = newMicrosoft.Phone.Marketplace.LicenseInformation();
    IsTrial = license.IsTrial();
#endif
}

Step6: Add the following code in Application_Launching handler:

?
1
2
3
4
privatevoid Application_Launching(objectsender, LaunchingEventArgs e)
{
    this.CheckTrialState();
}

Step7: Add the following code in Application_Activated handler:

?
1
2
3
4
privatevoid Application_Activated(objectsender, ActivatedEventArgs e)
{
    this.CheckTrialState();
}

Step7: Build and run the project

NOTE: When running the sample project attached at the end of the article do not forget to choose "DebugTrial" configuration. For reference take a look at Part1 of this article.

To be able to simulate and test a real Trial experience follow the steps:

  • 1. Run the app
  • 2. Press Home button
  • 3. Press Back button

Here is how the final result should look like:

91-1391- 66

NOTE: UPDATE: When you test Trial application Buy Now for a real application you must seta a valid ContentIdentifier (the unique Marketplace ID of your app):

?
1
2
3
4
MarketplaceDetailTask task = newMarketplaceDetailTask();
task.ContentType = MarketplaceContentType.Applications;
task.ContentIdentifier = "82a23635-5bd9-df11-a844-00237de2db9e";
task.Show();

That was all about how to implement "Buy Now" functionality for your WP7 Trial application with automatically show/hide message depending on the calculated time since the Trial first launch date. Here is the full source code:

WP7TrialAppAdvanced

I hope that the article was helpful.

You can also follow us on Twitter @winphonegeek

Comments

Good series

posted by: Nattanahel Chaves on 5/18/2011 8:23:14 PM

I have read all of theme, and was helpful!! thank you.

Exactly what I was looking for!

posted by: Tomash Vahner on 5/20/2011 9:13:03 AM

Thanks guys that was exactly what I was looking for! Keep up with the good work.

Cool stuff

posted by: Peter T. on 5/20/2011 9:21:02 AM

Very interesting approach. I was wondering how to add something similar to my app. Definitely will give it a try.

Trial Version Restart after re-install

posted by: GL on 9/17/2011 8:46:18 PM

Is it true that if a user uninstalled the app and re installed it they can restart the trial period again?

posted by: someName on 10/17/2011 11:00:15 PM

@GL seems to. what a bummer

How to get the product ID before submitting

posted by: Asad on 11/11/2011 7:32:06 PM

Hi: How can I get the product ID before I submit to the market place? I see that there is a "product ID" in the WMAppManifest.xaml but that does not match the product ID of the previous version that I submitted of the same app.

The product ID in the WMAppManifest.xaml didn't change in my solution yet the app in the market place has a different ID... Now that I am creating a new app and I want to use the code above, how do I get the app ID before I submit it to the market place?

Thanks Asad

Marketplace submission error

posted by: pg on 5/5/2012 10:40:24 AM

Thanks for the article, great stuff!

But, because of the back button not in function on buy now popup, i got marketplace submission error:

Comments: When the user presses the device Back button, the dialog box does not close and return the user to the screen where the dialog box was opened. Steps to Reproduce: 1. Install the trial version of the application. 2. Launch the application. 3. Use the application until the "upgrade to full version" dialog box appears. 4. Press the device Back button. 5. Observe the dialog box does not close and return the user to the screen where the dialog box was opened.

Just fyi.

for submission error

posted by: pg on 5/5/2012 12:07:24 PM

I made these changes (changes indicated by *):

public partial class ucBuyNowPage : UserControl { private Popup popup = null;

public ucBuyNowPage()

* : this(null,null) { }

  • public ucBuyNowPage(Popup popup, PhoneApplicationPage pg) { InitializeComponent(); this.popup = popup;

  • if (pg != null)

  • {
  • pg.BackKeyPress += new EventHandler(pg_BackKeyPress);
  • }
  • }

  • void pg_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)

  • {
  • if (null != this.popup && true == this.popup.IsOpen)
  • {
  • e.Cancel = true;
  • ClosePopup();
  • }
  • }

    private void btnBuyNow_Click(object sender, RoutedEventArgs e) { App.buyApp(); }

    private void btnContinueTrial_Click(object sender, RoutedEventArgs e) { this.ClosePopup(); }

    private void ClosePopup() { if (null != this.popup) { this.popup.IsOpen = false; } } }

void RootFrameNavigated(object sender, NavigationEventArgs e) { // remove the Navigated event handler as it is no longer necessary this.RootFrame.Navigated -= new NavigatedEventHandler(RootFrameNavigated);

Popup popup = new Popup();
  • PhoneApplicationPage pg = (PhoneApplicationPage)RootFrame.Content;
  • ucBuyNowPage content = new ucBuyNowPage(popup, pg);

    // set the width of the popup to the width of the screen content.Width = System.Windows.Application.Current.Host.Content.ActualWidth; popup.Child = content; popup.VerticalOffset = 300; popup.IsOpen = true;

}

the code again

posted by: pg on 5/5/2012 12:14:38 PM

public partial class ucBuyNowPage : UserControl{private Popup popup = null;public ucBuyNowPage()    : this(null,null){}public ucBuyNowPage(Popup popup, PhoneApplicationPage pg){    InitializeComponent();    this.popup = popup;    if (pg != null)     {        pg.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(pg_BackKeyPress);    } }void pg_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e){    if (null != this.popup && true == this.popup.IsOpen)    {        e.Cancel = true;        ClosePopup();    }}private void btnBuyNow_Click(object sender, RoutedEventArgs e){    App.buyApp();}private void btnContinueTrial_Click(object sender, RoutedEventArgs e){    this.ClosePopup();}private void ClosePopup(){    if (null != this.popup)    {        this.popup.IsOpen = false;    }}

}

void RootFrame_Navigated(object sender, NavigationEventArgs e){    // remove the Navigated event handler as it is no longer necessary    this.RootFrame.Navigated -= new NavigatedEventHandler(RootFrame_Navigated);    Popup popup = new Popup();    PhoneApplicationPage pg = (PhoneApplicationPage)RootFrame.Content;    ucBuyNowPage content = new ucBuyNowPage(popup, pg);    // set the width of the popup to the width of the screen    content.Width = System.Windows.Application.Current.Host.Content.ActualWidth;    popup.Child = content;    popup.VerticalOffset = 300;    popup.IsOpen = true;}
原创粉丝点击