如何在基于Desktop Bridge的WPF应用中调用Microsoft Store API实现应用内购买?
Absolutely! You absolutely can implement in-app purchases (IAPs) in your WPF app once it's packaged with the Desktop Bridge for the Microsoft Store. Let me walk you through the key steps and how to make this work smoothly:
First, you need to package your WPF app as an MSIX package (the core of the Desktop Bridge). This packaging step is non-negotiable—it’s what gives your app the identity and permissions needed to interact with Microsoft Store APIs.
Since WPF is a .NET desktop framework, you’ll need to reference the Windows Runtime (WinRT) APIs that handle Store interactions. Do this by installing the Microsoft.Windows.SDK.Contracts NuGet package into your WPF project. This package bridges the gap between .NET and the WinRT APIs required for IAPs.
In Visual Studio, right-click your WPF project → select Publish → choose Associate App with the Store. Follow the prompts to link your project to the app you’ve created in the Microsoft Store Partner Center. This links your packaged app to your Store listing and any IAP products you’ll create.
Head to the Microsoft Store Partner Center, navigate to your app’s dashboard, and create your IAP products (choose from consumable, non-consumable, or subscription types). Make sure to note each product’s Product ID—you’ll need this to reference the items in your code.
Here’s a simplified example of how to handle purchases and license checks:
Initialize the Store Context
First, get a reference to the StoreContext—this is your main entry point for Store API calls. For desktop apps, use StoreContext.GetDefault():
using Windows.Services.Store; // Initialize the Store context var storeContext = StoreContext.GetDefault();
Handle Purchases
Use RequestPurchaseAsync to initiate a purchase flow for a specific IAP product. Since this is an async operation, make sure to handle it properly in your WPF UI:
private async void BuyButton_Click(object sender, RoutedEventArgs e) { string productId = "your-product-id-here"; // Replace with your actual Product ID StorePurchaseResult result = await storeContext.RequestPurchaseAsync(productId); switch (result.Status) { case StorePurchaseStatus.Succeeded: // Purchase went through—unlock the feature or content for the user MessageBox.Show("Purchase successful!"); break; case StorePurchaseStatus.AlreadyPurchased: MessageBox.Show("You already own this item!"); break; default: MessageBox.Show($"Purchase failed: {result.ExtendedError.Message}"); break; } }
Check Existing Licenses
On app startup, verify if the user already owns a non-consumable IAP or active subscription to unlock features automatically:
private async void CheckLicensesOnStartup() { StoreAppLicense appLicense = await storeContext.GetAppLicenseAsync(); foreach (var addOnLicense in appLicense.AddOnLicenses) { if (addOnLicense.Key == "your-product-id-here" && addOnLicense.Value.IsActive) { // User owns this IAP—enable the relevant feature premiumFeaturePanel.Visibility = Visibility.Visible; } } }
- Testing: Use test accounts in the Partner Center’s sandbox environment to test purchases without real money.
- .NET Framework vs .NET 6+: For older .NET Framework WPF apps, ensure the WinRT references are set to
Copy Local = Truein your project references. - Identity Matching: Double-check that your MSIX package’s identity (publisher, app name) matches exactly what’s configured in the Partner Center—mismatches will break API calls.
内容的提问来源于stack exchange,提问作者Neha

