應用內購買是目前最容易变现的模式之一,和 7.5 提供的「試用」有異曲同工之處,但 WP8 的做法更貼近使用習慣,也帶來更多機會。商品分兩類:持久型(Durable),例如遊戲中的裝備;消耗型(Consumable),例如食品與金幣。手機錢包中還可以綁定支付寶帳戶,用於應用購買與支付。
本文是「升級到 WP8 必你如果知道的 13 個特性」系列的一篇。
升級到 WP8 必你如果知道的 13 個特性系列目錄:https://www.linuxidc.com/Linux/2013-08/89003.htm
核心是 CurrentApp 類別;若尚未通過審核,可改用 CurrentAppSimulator 搭配本地 XML 模擬資料測試。常用方法是 LoadListingInformationAsync():
private async void btnListIAPProducts_Tap(object sender,
System.Windows.Input.GestureEventArgs e)
{
try
{
var ProdList = await CurrentApp.LoadListingInformationAsync();
lbProductsList.Items.Clear();
string t = "";
foreach (var item in ProdList.ProductListings)
{
t = string.Format("{0}, {1}, {2}, {3}, {4}",
item.Key, item.Value.Name, item.Value.FormattedPrice,
item.Value.ProductType, item.Value.Description);
lbProductsList.Items.Insert(0, t);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
這段會把商品名稱、顯示價格、類型與描述逐筆列出來。真正的購買動作則是呼叫 CurrentApp.RequestProductPurchaseAsync(productId),並在回傳結果中判斷是否成功;消耗型商品在交付內容後,記得再呼叫 ReportConsumableCompletionAsync,否則使用者重開應用時可能被再次扣款。
實務上強烈建議先把購買狀態持久化到本地或雲端,避免使用者重裝後權益遺失。