Windows 8 應用商店提供豐富的獲利模式:試用版、一次性購買、應用程式內購買、第三方電子商務、廣告。本實驗示範使用 Windows.ApplicationModel.Store 命名空間下的 API,配合 CurrentAppSimulator 進行開發測試。
Data/license.xml(含價格、試用狀態、產品定義)App.xaml.cs OnLaunched 初始化模擬器:var file = await Package.Current.InstalledLocation.GetFileAsync("Data\\license.xml");
await CurrentAppSimulator.ReloadSimulatorAsync(file);
AppLicenseDataSource 實作 INotifyPropertyChanged,公開 IsTrial、FormattedPrice、LicenseInfo 屬性,監聽 LicenseChanged 事件更新 UI。AboutUserControl.xaml 繫結:<TextBlock Text="{Binding LicenseInfo, Source={StaticResource License}}"/>
<Button Visibility="{Binding IsTrial, Converter={StaticResource BooleanToVisibilityConverter}}"
Content="{Binding FormattedPrice, Source={StaticResource License}}"
Click="OnPurchaseButtonClicked"/>
private void OnPurchaseButtonClicked(object sender, RoutedEventArgs e)
{
CurrentAppSimulator.RequestAppPurchaseAsync(false);
}
// LicenseChanged 事件會自動觸發 UI 更新,按鈕消失、顯示授權到期日
ProductLicenseDataSource 封裝產品 ItalianRecipes 的授權狀態。ItemDetailPage 繫結:未購買顯示購買按鈕,已購買顯示料理步驟。private void OnPurchaseProduct(object sender, RoutedEventArgs e)
{
if (CurrentAppSimulator.LicenseInformation.IsTrial)
new MessageDialog("請先升級完整版").ShowAsync();
else
CurrentAppSimulator.RequestProductPurchaseAsync("ItalianRecipes", false);
}
| 類別 | 用途 | 實機上架替換為 |
|---|---|---|
CurrentAppSimulator | 開發測試,讀取 license.xml | CurrentApp |
LicenseInformation.IsTrial | 判斷試用版 | 同 |
RequestAppPurchaseAsync | 購買完整應用 | 同 |
RequestProductPurchaseAsync | 購買應用內產品 | 同 |
LoadListingInformationAsync | 取得商店列表資訊(價格、描述) | 同 |
CurrentAppSimulator 替換為 CurrentApp。ItalianRecipes)。