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

本文介绍如何为Windows Phone 7应用程序创建高级“立即购买”功能,包括根据首次启动时间自动显示和隐藏消息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

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:

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
< Grid Background = "Black" >
     < Grid.RowDefinitions >
         < RowDefinition Height = "90" />
         < RowDefinition Height = "*" />
     </ Grid.RowDefinitions >
     < Border Grid.Row = "0" >
     < TextBlock   TextWrapping = "Wrap" FontSize = "32" Padding = "10"
                    Text = "Do not forget to buy the full version!" />
     </ Border >
     < StackPanel Grid.Row = "1" Orientation = "Horizontal" HorizontalAlignment = "Center" >
         < Button x:Name = "btnBuyNow" Content = "Buy Now" Click = "btnBuyNow_Click" />
         < Button x: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
public partial class BuyNowUserControl : UserControl
{
     private Popup popup = null ;
 
     public BuyNowUserControl()
         : this ( null )
     {
     }
 
     public BuyNowUserControl(Popup popup)
     {
         InitializeComponent();
         this .popup = popup;
     }
 
     private void btnBuyNow_Click( object sender, RoutedEventArgs e)
     {
         MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
         marketplaceDetailTask.Show();
         this .ClosePopup();
     }
 
     private void btnContinueTrial_Click( object sender, RoutedEventArgs e)
     {
         this .ClosePopup();
     }
 
     private void 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
private static TimeSpan TrialPeriodLength = TimeSpan.FromSeconds(10.0);
private const 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
private void 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, out firstLauchDate))
     {
         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 += new NavigatedEventHandler(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
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();
     BuyNowUserControl content = new BuyNowUserControl(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
private void 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
public static bool IsTrial
{
     get ;
     // setting the IsTrial property from outside is not allowed
     private set ;
}
 
private void DetermineIsTrial()
{
#if TRIAL
     // return true if debugging with trial enabled (DebugTrial configuration is active)
     IsTrial = true ;
#else
     var license = new Microsoft.Phone.Marketplace.LicenseInformation();
     IsTrial = license.IsTrial();
#endif
}

Step6: Add the following code in Application_Launching handler:

?
1
2
3
4
private void Application_Launching( object sender, LaunchingEventArgs e)
{
     this .CheckTrialState();
}

Step7: Add the following code in Application_Activated handler:

?
1
2
3
4
private void Application_Activated( object sender, 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 = new MarketplaceDetailTask();
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:

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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值