您的位置:首页 > 移动开发

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

2010-12-01 18:23 375 查看
By Jeff Blankenburg

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

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

 

使用LicenseInformation类


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

using Microsoft.Phone.Marketplace;


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

LicenseInformation li = new LicenseInformation();


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

if (!li.IsTrial())
{
//Do something that only paid users can do.
}
else
{
//Do something that all users, trial or paid, can do.
}


测试试用模式


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

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

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
public App()
{
// Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;
settings["trialMode"] = false;
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
settings["trialMode"] = true;

// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;
// Show the areas of the app that are being redrawn in each frame.
//Application.Current.Host.Settings.EnableRedrawRegions = true;
// Enable non-production analysis visualization mode,
// which shows areas of a page that are being GPU accelerated with a colored overlay.
//Application.Current.Host.Settings.EnableCacheVisualization = true;
}
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
}


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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Marketplace;
using System.IO.IsolatedStorage;
namespace Day23_UsingTrial
{
public partial class MainPage : PhoneApplicationPage
{
LicenseInformation li = new LicenseInformation();
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

// Constructor
public MainPage()
{
InitializeComponent();
if (!li.IsTrial()||(bool)settings["trialMode"] == false)
{
//Do something that only paid users can do.
}
else if (li.IsTrial() || (bool)settings["trialMode"] == true)
{
//Do something that all users, trial or paid, can do.
}
}
}
}


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

 

下载示例代码


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





原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-23-Providing-Trial-Versions-of-Your-App.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐