参考:https://www.cnblogs.com/hupo376787/p/7733791.html
UWP商店内购相关,检查APP价格信息,检查加载项价格信息,购买加载项
namespace EasyNote.Helper
{
public class AppStore
{
private static StoreContext context = null;
public async Task<string> GetAppInfo()
{
var result = string.Empty;
if (context == null)
{
context = StoreContext.GetDefault();
}
StoreProductResult queryResult = await context.GetStoreProductForCurrentAppAsync();
if (queryResult.Product == null)
{
// The Store catalog returned an unexpected result.
result = "Something went wrong, and the product was not returned";
// Show additional error info if it is available.
if (queryResult.ExtendedError != null)
{
result += $"\nExtendedError: {queryResult.ExtendedError.Message}";
}
}
Debug.WriteLine(result);
return result;
}
/// <summary>
/// 获取产品内购信息
/// </summary>
public async Task<List<StoreProduct>> GetAddOnInfo()
{
var result = new List<StoreProduct>();
if (context == null)
{
context = StoreContext.GetDefault();
}
//指定加载项的类型
string[] productKinds = { "Durable", "Consumable", "UnmanagedConsumable" };
List<String> filterList = new List<string>(productKinds);
StoreProductQueryResult queryResult = await context.GetAssociatedStoreProductsAsync(filterList);
if (queryResult.ExtendedError != null)
{
Debug.WriteLine($"ExtendedError: {queryResult.ExtendedError.Message}");
}
Debug.WriteLine($"共有加载项{queryResult.Products.Count}个");
foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
{
// Access the Store product info for the add-on.
StoreProduct product = item.Value;
result.Add(product);
Debug.WriteLine(product.Title + product.Price);
// Use members of the product object to access listing info for the add-on...
}
return result;
}
public async void GetUserCollection()
{
if (context == null)
{
context = StoreContext.GetDefault();
// If your app is a desktop app that uses the Desktop Bridge, you
// may need additional code to configure the StoreContext object.
// For more info, see https://aka.ms/storecontext-for-desktop.
}
// Specify the kinds of add-ons to retrieve.
string[] productKinds = { "Durable" };
List<String> filterList = new List<string>(productKinds);
//workingProgressRing.IsActive = true;
StoreProductQueryResult queryResult = await context.GetUserCollectionAsync(filterList);
//workingProgressRing.IsActive = false;
if (queryResult.ExtendedError != null)
{
// The user may be offline or there might be some other server failure.
Debug.WriteLine($"ExtendedError: {queryResult.ExtendedError.Message}");
return;
}
foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
{
StoreProduct product = item.Value;
// Use members of the product object to access info for the product...
}
}
public async Task<string> PurchaseAddOn(string storeId)
{
string PurchaseResult = string.Empty;
if (context == null)
{
context = StoreContext.GetDefault();
}
//workingProgressRing.IsActive = true;
StorePurchaseResult result = await context.RequestPurchaseAsync(storeId);
//workingProgressRing.IsActive = false;
// Capture the error message for the operation, if any.
if (result.ExtendedError != null)
{
PurchaseResult = result.ExtendedError.Message;
}
switch (result.Status)
{
case StorePurchaseStatus.AlreadyPurchased:
PurchaseResult+= "The user has already purchased the product.";
UserSetting us = new UserSetting();
us.IsDonate = true;
break;
case StorePurchaseStatus.Succeeded:
PurchaseResult += "The purchase was successful.";
UserSetting us2 = new UserSetting();
us2.IsDonate = true;
break;
case StorePurchaseStatus.NotPurchased:
PurchaseResult += "The purchase did not complete. " + "The user may have cancelled the purchase. ExtendedError: ";
break;
case StorePurchaseStatus.NetworkError:
PurchaseResult += "The purchase was unsuccessful due to a network error. " ;
break;
case StorePurchaseStatus.ServerError:
PurchaseResult += "The purchase was unsuccessful due to a server error. " ;
break;
default:
PurchaseResult += "The purchase was unsuccessful due to an unknown error. ";
break;
}
return PurchaseResult;
}
}
}