質問

I've been having trouble implementing IAP for Windows Phone 8 on Monogame. I've been following the API documentation: WP8 In-App Purchase API

As Monogame/XNA updates differently to Silverlight apps, I'm running IAP on the UI thread. I've tested with a beta app on the store, and can load the Store Front UI and display my product. When I attempt to purchase, it seems like it makes the purchase, tries to go back to the game but crashes. Further loads of the app don't display the store at all after this.

I've searched everywhere for a solution for this with no luck. I'd be really grateful if anyone could help me with this.

Here is my code sample:

public static void InvokeOnUIThread(Action action)
    {
        #if WINDOWS8 || OPENGL
        action();
        #else
        var dispatcher = System.Windows.Deployment.Current.Dispatcher;

        if (dispatcher.CheckAccess() /*IsUIThread*/)
            action();
            else
            dispatcher.BeginInvoke(action);
        #endif
    }

I use it to invoke InvokeOnUIThread(Part1Purchase);

    async void Part1Purchase()
    {
        ListingInformation li = await CurrentApp.LoadListingInformationAsync();
        string pID = li.ProductListings["img.1"].ProductId;
        try
        {
            string receipt = await Windows.ApplicationModel.Store.CurrentApp.RequestProductPurchaseAsync(pID, false);

            Part2Fulfillment();
        }
        catch (Exception ex)
        {}
    }

    public void Part2Fulfillment()
    {
        var productLicenses = Windows.ApplicationModel.Store.CurrentApp.LicenseInformation.ProductLicenses;
        Part3DeliverGold(productLicenses["img.1"], 50);
    }

    void Part3DeliverGold(ProductLicense license, int goldCount)
    {
        if (license.IsConsumable && license.IsActive)
        {
            gold += goldCount;
            CurrentApp.ReportProductFulfillment(license.ProductId);
        }
    }

I also tries this method to execute on the UI thread, but had the same results (crashing on purchase completion)

    private void RestoreTitleDelayed(int delay)
    {
        ThreadPool.QueueUserWorkItem((object o) =>
            {
                Thread.Sleep(delay);
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    Part1Purchase();
                });
            });
    }
役に立ちましたか?

解決

I'm not sure what is exactly wrong with your situation. But we have a MonoGame project that is already in the store and in-apps work great. We had crashes while interacting with Store as well but eventually got it working. This is our current approach:

In GamePage.xaml.cs we have:

...
using StoreExitAction = System.Action<string, string, bool>;
...
private static StoreExitAction StoreExitCallback;
....

   public void LaunchStoreForProductPurchase(string productID, bool requestReceipt, StoreExitAction storeExitCallback)
    {

         StoreExitCallback = storeExitCallback;
        Dispatcher.BeginInvoke(() => LaunchStoreForProductPurchaseASync(productID, requestReceipt));
    }

    private static async void LaunchStoreForProductPurchaseASync(string productID, bool requestReceipt)
    {
        bool productPurchaseError = false;
        string receipt = "";

        try {
            receipt = await Store.CurrentApp.RequestProductPurchaseAsync(productID, requestReceipt);
        } 
        catch (Exception ex)
        {
            productPurchaseError = true;
        }

        try
        {
            if (StoreExitCallback != null) StoreExitCallback(productID, receipt, productPurchaseError);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

To start in-app purchase you do:

GamePage.Instance.LaunchStoreForProductPurchase(productID, true, ReturnFromStoreCallback);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top