locked
In App Purchases RRS feed

  • Question

  • 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.



    • Edited by damichab Friday, April 24, 2020 5:55 AM
    • Moved by CoolDadTx Friday, April 24, 2020 2:27 PM UWA related
    Friday, April 24, 2020 5:53 AM

All replies

  • This forum is for C#-specific questions. Your question is really related to working with the MS Store API and UWP. Please post your question in the new Microsoft Q&A forums for UWP.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 24, 2020 2:26 PM