5

アプリ内購入用に次のコードを試しました。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));
    }
}

history_Clickただし、 S_OKを選択しても、実行するたびにダイアログがポップアップし続けます。をlicenseInformation.ProductLicenses["PremiumFeatures"].IsActive購入した後、true に変更されるはずPremiumFeaturesです。

ここに画像の説明を入力

IsActiveおそらく、アプリ内購入が成功したときに、フラグをオンにして、購入ダイアログが再度表示されないようにする必要があると思います。

        // the in-app purchase was successful
        OK = true;
        // Compile error!!!
        licenseInformation.ProductLicenses["PremiumFeatures"].IsActive = true;

わかった。IsActive読み取り専用フィールドのようです。では、購入が成功した場合の正しい対処法を教えてください。

アップデート :

試用版アプリとアプリ内購入のサンプルを見た後、購入が成功した後、次のコードがからに自動的IsActiveに変更されている可能性があることに気付きました。(しかし、なぜそれが機能するのですか?)falsetrue

    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 + ".";
    }

ただし、「購入済み」の状態は永続ではありません。アプリを閉じて再度起動すると、「プレミアム機能」は「まだ購入されていません」と見なされます。

この動作を永続化するにはどうすればよいでしょうか?

4

5 に答える 5

8

読んでください

あなたの経験に基づいて、 App.IsTrial が true に設定されていると思います。アプリが試用モードにある間、または (その他の理由で) アクティブでない間は、購入をコミットできません。機能/製品ではなく、アプリについて言及していることに注意してください。アプリがアクティブになると、購入が成功します。

于 2013-02-20T05:45:53.567 に答える
3

私の知る限り、[OK] をクリックしても自動的に更新されません。Windows ストアで行う必要があるのと同じように、正しいステータスのみを返します。

私が行うオプションだと思いますが、自分で実装していないのは、ReloadSimulatorAsync(proxyFile)メソッドを使用することです。このメソッドを呼び出す前に、in-app-purchase.xml を自分で更新して保存し、そのファイルを ReloadSimulatorAsync メソッドに渡す必要があります。

個人的には、xml ファイルの手動更新を使用します。しかし、あなたのアプリケーション/アプリケーションフローについては判断できません。

このプロセスを自動化するための良い出発点は、ここで見つけることができるサンプルの「アプリ内購入」アプリです。コード ファイル InAppPurchase.xaml.cs を見ると、独自の実装用のコードが既に少しあることがわかります。

于 2012-12-20T10:31:22.960 に答える
0

私はこれをクリアするつもりです。実行時

  try {

        await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
        OK = true

    }
    catch (Exception)
    {

        OK = false;
    }

OK は、ユーザーがその機能を購入した場合だけでなく、購入をキャンセルしたり、身元を特定できなかったり、ネットワーク接続を無効にしたりした場合にも当てはまります。したがって、これは購入が成功したかどうかを確認する間違った方法です。基本的に Anobik は正しかったので、後でライセンスを購入したかどうかを確認する必要があるため、正しいコードは次のようになります。

if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
{
    try
    {
        // The customer doesn't own this feature, so 
        // show the purchase dialog.

        await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
                if (licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
                {
                    // the in-app purchase was successful
                     OK = true;
                    //Success in purchase use ur own code block
                }
                else
                {
                     OK = false;
                    //error in purchase use your own code block
                }

    }
    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));
}

それでも、コンパイルすると、機能しないことがわかります。これは、CurrentAppSimulator がライセンスを永続的に変更しないためだと思います。これが本当であることを願っています:)

于 2013-03-26T18:51:38.930 に答える
-1

アプリ内購入を処理するための if else 条件が 1 つだけ欠けています

これがコードです

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);
                if (licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
                {
                    // the in-app purchase was successful
                     OK = true;
                    //Success in purchase use ur own code block
                }
                else
                {
                     OK = false;
                    //error in purchase use your own code block
                }

    }
    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));
}

}

于 2012-12-20T12:20:38.343 に答える