尽管 AdDuplex Ad Control 折叠它仍继续显示

AdDuplex Ad Control keeps showing despite collapsing it

我正在创建一个 WinRT(Windows 8.1 和 Windows Phone 8.1)应用程序,我在其中一个页面中放置了一个 AdDuplex 广告控件。

应用程序的用户可以选择删除广告(使用 IAP)。当他们这样做时,我将 AdDuplex 广告控件的 Visibility 从页面 ViewModel 设置为 Collapsed

这部分工作正常;然而,一段时间后,当用户仍在页面上时,AdDuplex 广告控件突然再次可见并开始显示广告。

一开始,我认为这是使用 CurrentAppSimulator 时 IAP 的行为,虽然这对我来说没有意义,因为我在代码中没有任何内容可以对许可证更改做出反应并因此设置控件回到 Visible。然而,我为我的“NoAd”产品测试了license.IsActive并得到了true,表明许可证有效。

以下是我的代码的简化部分:

MyPage.xaml

<ad:AdControl
    AdUnitId="{StaticResource AdUnitId}"
    AppKey="{StaticResource AdAppKey}"
    IsTest="True"
    CollapseOnError="True"
    Visibility="{Binding IsNoAdPurchased, Converter={StaticResource BooleanToVisibilityInvertedConverter}}"/>

MyPageViewModel.cs

private async void RemoveAd()
{
    this.IsNoAdPurchased = await this.storeService.PurchaseProductAsync(Products.NoAd);
}

StoreService.cs

#if DEBUG
using StoreCurrentApp = Windows.ApplicationModel.Store.CurrentAppSimulator;
#else
using StoreCurrentApp = Windows.ApplicationModel.Store.CurrentApp;
#endif

public sealed class StoreService
{
    public async Task<bool> PurchaseProductAsync(string productId)
    {
        try
        {
            var purchase = await StoreCurrentApp.RequestProductPurchaseAsync(productId);
            return purchase.Status == ProductPurchaseStatus.Succeeded || purchase.Status == ProductPurchaseStatus.AlreadyPurchased;
        }
        catch (Exception)
        {
            // The purchase did not complete because an error occurred.
            return false;
        }
    }
}

我做了一个demo,后面跟着你的,你可以参考一下。

xaml部分:

<Page.Resources>
    <local:MyConverter x:Key="myconverter"></local:MyConverter>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Windows81:AdMediatorControl x:Name="AdMediator" HorizontalAlignment="Left" Height="250" Id="AdMediator-Id-FA61D7FD-4F5F-445D-AB97-DB91618DBC70" Margin="557,287,0,0" VerticalAlignment="Top" Width="300" Visibility="{Binding IsVisible,Converter={StaticResource myconverter}}" />
    <Button Name="btn1" Content="Remove ad" Click="RemoveAd" Visibility="Visible" />
</Grid>

隐藏代码:

public class Recording : INotifyPropertyChanged
    {
        private bool isVisible;

        public Recording()
        {
        }

        public bool IsVisible
        {
            get
            {
                return isVisible;
            }

            set
            {
                if (value != isVisible)
                {
                    isVisible = value;
                    OnPropertyChanged("IsVisible");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private Recording recording;

        public MainPage()
        {
            this.InitializeComponent();
            Init();
            recording = new Recording();
            recording.IsVisible = false;
            this.DataContext = recording;

        }

        private async void Init()
        {
            StorageFile proxyFile = await Package.Current.InstalledLocation.GetFileAsync("in-app-purchase.xml");
            await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
        }

        public async Task<bool> PurchaseProductAsync(string productId)
        {
            try
            {
                var purchase = await CurrentAppSimulator.RequestProductPurchaseAsync(productId);
                return purchase.Status == ProductPurchaseStatus.Succeeded || purchase.Status == ProductPurchaseStatus.AlreadyPurchased;
            }
            catch (Exception)
            {
                // The purchase did not complete because an error occurred.
                return false;
            }
        }

        private async void RemoveAd(object sender, RoutedEventArgs e)
        {
            recording.IsVisible = await this.PurchaseProductAsync("product2");
        }


    }

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is Boolean && (bool)value)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

我已经测试过了,购买产品后,广告将不再显示。

另外我想建议你使用另一种不绑定的方法。

xaml部分:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Windows81:AdMediatorControl x:Name="AdMediator" HorizontalAlignment="Left" Height="250" Id="AdMediator-Id-FA61D7FD-4F5F-445D-AB97-DB91618DBC70" Margin="557,287,0,0" VerticalAlignment="Top" Width="300" Visibility="Visible" />
        <Button Name="btn1" Content="Remove ad" Click="Button_Click" Visibility="Visible" />
</Grid>

隐藏代码:

namespace AdmediatorTest
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Init();

            LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
            if (!licenseInformation.ProductLicenses["product2"].IsActive)
            {
                btn1.Visibility = Visibility.Visible;
            }
            else
            {
                btn1.Visibility = Visibility.Collapsed;
            }
        }

        private async void Init()
        {
            StorageFile proxyFile = await Package.Current.InstalledLocation.GetFileAsync("in-app-purchase.xml");
            await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
            if (!licenseInformation.ProductLicenses["product2"].IsActive)
            {
                try
                {
                    await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
                    if (licenseInformation.ProductLicenses["product2"].IsActive)
                    {
                        AdMediator.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        AdMediator.Visibility = Visibility.Visible;
                    }
                }
                catch (Exception)
                {
                    //rootPage.NotifyUser("Unable to buy " + productName + ".", NotifyType.ErrorMessage);
                }
            }
            else
            {
                //rootPage.NotifyUser("You already own " + productName + ".", NotifyType.ErrorMessage);
            }
        }
    }
}

另外我找到了一个awesome video关于IAP后去除广告的,你也可以参考一下。

这是 AdDuplex 广告控件的一个问题,已在版本 9.0.0.13 中修复。

注意:不要忘记将 IsTest 设置为 false 以查看 "production" 行为。