我认为这里的问题在于你需要区分测试和生产环境。除非你已经发布到商店,否则不能使用生产模式。请参阅
CurrentApp 和
CurrentAppSimulator。
从
CurrentAppSimulator
页面上可以看到:
备注
在应用程序被列在 Windows 商店之前,CurrentApp 对象将无法在应用程序中工作。在开发应用程序时,请使用 CurrentAppSimulator 测试您的应用程序许可证和应用内产品。在测试应用程序之后,并在提交到 Windows 商店之前,您必须将 CurrentAppSimulator 的实例替换为 CurrentApp。如果应用程序使用 CurrentAppSimulator,则应用程序将无法通过认证。
以下是我在我的应用程序中使用的 #define 解决此问题的方法,我根据测试/生产更改它,并使用一个代理类在 CurrentApp 和 CurrentAppSimulator 之间切换,以使我的其他代码更容易阅读。
App.xaml.cs, App()
#if false
#warning WARNING: You are using CurrentAppProxy in TEST MODE!
CurrentAppProxy.SetTestMode(true);
#else
CurrentAppProxy.SetTestMode(false);
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()
CurrentAppProxy 是关键。如果它在模拟器上运行正常,那么它应该可以与您的生产项目一起使用。您只需为各种条件准备好所有其他组件即可。一些测试最容易在调试器中进行。
IReadOnlyDictionary
。这个应该在 Visual Studio 上应用程序上工作吗(我的意思是,不是在应用商店上)? - JBernardo