创建WP8试用应用

时间:2023-03-09 05:40:50
创建WP8试用应用

参考资料:

创建 Windows Phone 的试用应用

如何在 Windows Phone 应用中实现试用体验

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

对资料总结下:

如何检查应用中的试用许可证:

在App.xaml.cs中添加下面两行

using Microsoft.Phone.Marketplace;

private static LicenseInformation _licenseInfo = new LicenseInformation();

使用_licenseInfo.IsTrial()方法,即读取LicenseInformation对象上的IsTrial属性,返回True时为试用版,否则为付费版

但是实际上在调试时,应用的真实许可信息只有在发布到商店后才能使用,所以再在App.xaml.cs里使用以下两段代码。使用后,在调试时将执行#if Debug与#else之间的代码,进行手动设置是否为试用版;在释放模式下时,将执行#else与#endif之间的代码,即读取LicenseInformation对象上的IsTrial属性来设置是否试用版。

        private static bool _isTrial = true;
public bool IsTrial
{
get
{
return _isTrial;
}
}
        /// <summary>
/// Check the current license information for this application
/// </summary>
private void CheckLicense()
{
// When debugging, we want to simulate a trial mode experience. The following conditional allows us to set the _isTrial
// property to simulate trial mode being on or off.
#if DEBUG
string message = "This sample demonstrates the implementation of a trial mode in an application." +
"Press 'OK' to simulate trial mode. Press 'Cancel' to run the application in normal mode.";
if (MessageBox.Show(message, "Debug Trial",
MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
_isTrial = true;
}
else
{
_isTrial = false;
}
#else
_isTrial = _licenseInfo.IsTrial();
#endif
}

在应用开始或恢复时都需要检查应用的许可证:

即在App.xaml.cs中的Application_Launching和Application_Activated事件中添加试用版的判断方法CheckLicense()

使用当前应用的许可证信息:

由于在App.xaml.cs里设置了IsTrial属性来读取许可证属性,因此我们可以通过使用(Application.Current as App).IsTrial属性来直接获取应用的许可证信息,避免重复使用IsTrial()方法,该方法一次典型调用耗时60毫秒或更多。