我认为这里的问题是您需要区分测试和生产。除非您在商店中发布,否则您无法使用生产模式。见CurrentApp 和CurrentAppSimulator。
来自CurrentAppSimiulator 页面:
备注
在应用程序在 Windows 应用商店中列出之前,CurrentApp 对象将无法在应用程序中工作。在开发应用时,使用 CurrentAppSimulator 测试应用的许可和应用内产品。在测试您的应用程序之后,在将其提交到 Windows 应用商店之前,您必须将 CurrentAppSimulator 的实例替换为 CurrentApp。如果您的应用使用 CurrentAppSimulator,您的应用将无法通过认证。
以下是我如何在我的应用程序中使用 #define 来解决这个问题,我为测试/生产进行了更改,以及在 CurrentApp 和 CurrentAppSimulator 之间切换的代理类,以使我的其他代码更容易阅读。
App.xaml.cs, App()
//
// configure in-app purchasing
//
#if false
#warning WARNING: You are using CurrentAppProxy in TEST MODE!
CurrentAppProxy.SetTestMode(true); // true for test, false for production
#else
CurrentAppProxy.SetTestMode(false); // true for test, false for production
CurrentAppProxy.cs
public static class CurrentAppProxy
{
static bool? testmode = null;
public static async void SetTestMode(bool mode)
{
testmode = mode;
if (mode)
{
var file = await Package.Current.InstalledLocation.GetFileAsync("WindowsStoreProxy.xml");
if (file != null)
{
await CurrentAppSimulator.ReloadSimulatorAsync(file);
}
}
}
public static LicenseInformation LicenseInformation
{
get
{
if (testmode == null) throw new NotSupportedException();
else if (testmode.Value) return CurrentAppSimulator.LicenseInformation;
else return CurrentApp.LicenseInformation;
}
}
public static IAsyncOperation<IReadOnlyList<UnfulfilledConsumable>> GetUnfulfilledConsumablesAsync()
{
if (testmode == null) throw new NotSupportedException();
else if (testmode.Value) return CurrentAppSimulator.GetUnfulfilledConsumablesAsync();
else return CurrentApp.GetUnfulfilledConsumablesAsync();
}
public static IAsyncOperation<ListingInformation> LoadListingInformationAsync()
{
if (testmode == null) throw new NotSupportedException();
else if (testmode.Value) return CurrentAppSimulator.LoadListingInformationAsync();
else return CurrentApp.LoadListingInformationAsync();
}
public static IAsyncOperation<FulfillmentResult> ReportConsumableFulfillmentAsync(string productId, Guid transactionId)
{
if (testmode == null) throw new NotSupportedException();
else if (testmode.Value) return CurrentAppSimulator.ReportConsumableFulfillmentAsync(productId, transactionId);
else return CurrentApp.ReportConsumableFulfillmentAsync(productId, transactionId);
}
public static IAsyncOperation<PurchaseResults> RequestProductPurchaseAsync(string productId)
{
if (testmode == null) throw new NotSupportedException();
else if (testmode.Value) return CurrentAppSimulator.RequestProductPurchaseAsync(productId);
else return CurrentApp.RequestProductPurchaseAsync(productId);
}
}
在我的应用中使用...
private async Task RefreshInAppOffers()
{
// in-app offers
List<KeyValuePair<string, ProductListing>> products = null;
UnfulfilledConsumable unfulfilledConsumable = null;
InAppOffers.Children.Clear();
try
{
var listinginfo = await CurrentAppProxy.LoadListingInformationAsync();
products = listinginfo.ProductListings.ToList();
products.Sort((p1, p2) => p1.Value.FormattedPrice.CompareTo(p2.Value.FormattedPrice));
CurrentAppProxy 是这里的关键。如果它适用于模拟器,它应该适用于您的生产项目。您只需要为所有各种条件准备好所有其他部分。在调试器中进行一些测试是最容易的。