Windows Phone 7 开发 31 日谈——第23日:提供试用版应用程序

By Jeff Blankenburg

本文是 “Windows Phone 7 开发 31 日谈” 系列的第23日。

    昨天,我写了如何将游戏添加到电话的游戏中心中。今天,我会向你展示为应用程序添加试用内容是多么简单。例如,假设你创建了一个50关的游戏。可能你想让用户能免费体验前5关,但要想玩后面的,他们就需要购买这个游戏。本文就像你展示如何做到。

 

使用LicenseInformation类

    通过向我们的页面中添加Microsoft.Phone.Marketplace程序集和相应的名称空间,就可以访问LicenseInformation类了,它直接与程序的“付费”状态相关。

[c-sharp]  view plain copy
  1. using Microsoft.Phone.Marketplace;  

下一步是真正地使用LicenseInformation类,来创建一个实例:

[c-sharp]  view plain copy
  1. LicenseInformation li = new LicenseInformation();  

最后,LicenseInformation有一个非常棒的返回布尔值的方法叫IsTrial(),毫无悬念,它允许我们检测程序是否处于试用状态。你可以很方便地将它用于一个if语句,就像这样:

[c-sharp]  view plain copy
  1. if (!li.IsTrial())  
  2. {  
  3.     //Do something that only paid users can do.  
  4. }  
  5. else  
  6. {  
  7.     //Do something that all users, trial or paid, can do.  
  8. }  

测试试用模式

    不幸的是,没有一种用来在试用和已付款状态间切换的内建机制。不过这处理起来很简单。我使用了在App.xaml.cs文件中相同的if语句。用它来检测你是否在调试,如果是,创建一个被我叫做“trialMode”的IsolatedStorageSetting。

下面是整个App()方法,包括App.xaml.cs文件自动生成的代码。在下面的例子中,我将trialMode设为了TRUE。当你测试“已付费”模式时要将它关闭。

[c-sharp]  view plain copy
  1. IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;  
  2. public App()  
  3. {  
  4.     // Global handler for uncaught exceptions.   
  5.     UnhandledException += Application_UnhandledException;  
  6.     settings["trialMode"] = false;  
  7.     // Show graphics profiling information while debugging.  
  8.     if (System.Diagnostics.Debugger.IsAttached)  
  9.     {  
  10.         settings["trialMode"] = true;  
  11.                   
  12.         // Display the current frame rate counters.  
  13.         Application.Current.Host.Settings.EnableFrameRateCounter = true;  
  14.         // Show the areas of the app that are being redrawn in each frame.  
  15.         //Application.Current.Host.Settings.EnableRedrawRegions = true;  
  16.         // Enable non-production analysis visualization mode,   
  17.         // which shows areas of a page that are being GPU accelerated with a colored overlay.  
  18.         //Application.Current.Host.Settings.EnableCacheVisualization = true;  
  19.     }  
  20.     // Standard Silverlight initialization  
  21.     InitializeComponent();  
  22.     // Phone-specific initialization  
  23.     InitializePhoneApplication();  
  24. }  

回顾一下早先的代码,我需要修改if语句来处理这个新的IsolatedStorageSettings值。这次我包含了整个MainPage.xaml.cs文件,所以结合上下文你可以看到所有的内容。

[c-sharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using Microsoft.Phone.Marketplace;  
  14. using System.IO.IsolatedStorage;  
  15. namespace Day23_UsingTrial  
  16. {  
  17.     public partial class MainPage : PhoneApplicationPage  
  18.     {  
  19.         LicenseInformation li = new LicenseInformation();  
  20.         IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;  
  21.           
  22.         // Constructor  
  23.         public MainPage()  
  24.         {  
  25.             InitializeComponent();  
  26.             if (!li.IsTrial()||(bool)settings["trialMode"] == false)  
  27.             {  
  28.                 //Do something that only paid users can do.  
  29.             }  
  30.             else if (li.IsTrial() || (bool)settings["trialMode"] == true)  
  31.             {  
  32.                 //Do something that all users, trial or paid, can do.  
  33.             }  
  34.         }  
  35.     }  
  36. }  

这就是所有你需要做的,当然这并不是“最好的”处理这种问题的方法,但对我来说它的确可以工作。如果谁有什么好的方法,我很乐意去用。

 

下载示例代码

通过一个可以运行的例子来看以上所有内容,下载这个解决方案并研究它。这始终是学习的一个好方法。

clip_image001

原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-23-Providing-Trial-Versions-of-Your-App.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值