In App Purchases Problems

David Bailey 1 Reputation point
2020-04-24T21:45:18.547+00:00

I have written an app for the Microsoft Store. While I can see if license has been purchased or if still running under a trial period, I cannot for the life of me get the in-app purchase to work. I have followed various examples from both MS websites and elsewhere.

private void testButton_Click(object sender, EventArgs e)
{
    _ = SetStore();
}



[ComImport]
[Guid("4AEEEC08-7C92-4456-A0D6-1B675C7AC005")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IInitializeWithWindow
{
    void Initialize(IntPtr hwnd);
}

private async Task SetStore()
{
    try
    {
        StoreContext theStore = StoreContext.GetDefault();

        // "Unable to cast object of type 'Windows.Services.Store.StoreContext' to type 'IInitializeWithWindow'."
        // IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)theStore;
        // initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

        StoreProductResult app = await theStore.GetStoreProductForCurrentAppAsync();

        // This works...
        StoreProduct product = app.Product;
        string title = product.Title;
        string price = product.Price.FormattedPrice;

        // This works...
        StoreAppLicense license = await theStore.GetAppLicenseAsync();
        bool trial = license.IsTrial;
        bool full = license.IsActive;



        // "Invalid window handle.\r\n\r\nThis function must be called from a UI thread"
        //StorePurchaseResult result = await theStore.RequestPurchaseAsync("9NRFBVGVGW8K");

        // "Invalid window handle.\r\n\r\nThis function must be called from a UI thread" 
        // Windows.Foundation.IAsyncOperation<StorePurchaseResult> purchase = product.RequestPurchaseAsync();


    }
    catch (Exception e)
    {
        int a = 1;
    }

}

The app is written in .NET with a UWP wrapper to create the MSIX package. The rem'ed lines indicate where I am having issues. Can anyone help me with these?

Otherwise, do I have to offer in app purchase for a trial program or can I just direct the user back to the Microsoft Store when they are ready to buy? As stated above, the code that checks for a valid license is working well.

Universal Windows Platform (UWP)
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.