Che*_*eng 5 c# microsoft-metro windows-8
我尝试了以下代码进行应用内购买.我正在CurrentAppSimulator
用于测试目的.
private async void history_Click(object sender, RoutedEventArgs e)
{
bool OK = true;
// Get the license info
// The next line is commented out for testing.
// licenseInformation = CurrentApp.LicenseInformation;
// The next line is commented out for production/release.
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
{
try
{
// The customer doesn't own this feature, so
// show the purchase dialog.
await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
// the in-app purchase was successful
OK = true;
}
catch (Exception)
{
// The in-app purchase was not completed because
// an error occurred.
OK = false;
}
}
else
{
// The customer already owns this feature.
OK = true;
}
if (OK)
{
Frame.Navigate(typeof(HistoryPage));
}
}
Run Code Online (Sandbox Code Playgroud)
但是,每次执行时history_Click
,对话框都会弹出,即使我选择了S_OK.在我买完licenseInformation.ProductLicenses["PremiumFeatures"].IsActive
之后,我希望应该改为真PremiumFeatures
.
我想,也许当应用内购买成功时,我需要打开IsActive
标志,这样它就不会再显示购买对话框了.
// the in-app purchase was successful
OK = true;
// Compile error!!!
licenseInformation.ProductLicenses["PremiumFeatures"].IsActive = true;
Run Code Online (Sandbox Code Playgroud)
好.好像IsActive
是一个只读字段.那么,我可以知道,处理购买成功案例的正确方法是什么?
在查看试用版应用和应用内购买示例后,我意识到在购买成功后,以下代码可以自动IsActive
更改false
为true
自动.(但为什么它有效?)
private async Task LoadInAppPurchaseProxyFileAsync()
{
StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("data");
StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
// setup application upsell message
ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();
var product1 = listing.ProductListings["product1"];
var product2 = listing.ProductListings["product2"];
Product1SellMessage.Text = "You can buy " + product1.Name + " for: " + product1.FormattedPrice + ".";
Product2SellMessage.Text = "You can buy " + product2.Name + " for: " + product2.FormattedPrice + ".";
}
Run Code Online (Sandbox Code Playgroud)
但是,"已经购买"的状态并不是持久性的.如果我关闭应用程序并再次启动它,它会认为我的"高级功能"为"尚未购买".
我怎么想让这种行为持续存在?
请阅读
根据您的经验,我认为App.IsTrial设置为true.当应用程序处于试用模式或(由于任何其他原因)未激活时,您无法提交购买.请注意,我指的是应用程序,而不是功能/产品.应用激活后,您的购买将会成功.
据我所知,当您单击“确定”时,它不会自动更新。它只返回正确的状态,就像在 Windows 应用商店中应该做的那样。
我认为这是一种选择,但我自己还没有实现它是使用ReloadSimulatorAsync(proxyFile)
方法。在调用此方法之前,您必须自行更新 in-app-purchase.xml 并将其保存并将文件传递给 ReloadSimulatorAsync 方法。
我个人会使用手动更新 xml 文件。但我无法判断您的申请/申请流程。
自动化此过程的一个良好开端是示例“应用内购买”应用程序,您可以在此处找到它。如果您查看代码文件 InAppPurchase.xaml.cs,您将看到一些您自己实现的代码。
归档时间: |
|
查看次数: |
1759 次 |
最近记录: |