【问题标题】:AdDuplex Ad Control keeps showing despite collapsing it尽管折叠了 AdDuplex 广告控件,但它仍会继续显示
【发布时间】:2016-09-11 16:57:54
【问题描述】:

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

应用的用户可以选择移除广告(使用 IAP)。当他们这样做时,我从 ViewModel 页面将 AdDuplex 广告控件的 Visibility 设置为 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;
        }
    }
}

【问题讨论】:

    标签: c# windows-runtime windows-store-apps winrt-xaml adduplex


    【解决方案1】:

    我做了一个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后删除广告的内容,你也可以参考一下。

    【讨论】:

    • (1) 我面临的问题是 AdDuplex 而不是 AdMediator。我没有尝试过后者,(2)不确定为什么您的建议是使用代码隐藏而不是 ViewModel 以及它们在这种情况下有何不同,(3)“在我的机器上工作”对我没有多大帮助:) 和 (4) 这是一个重现问题的sample app(尽我所能使其与我的应用程序尽可能相似)。
    【解决方案2】:

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

    注意:不要忘记将IsTest 设置为false 以查看“生产”行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      相关资源
      最近更新 更多