如何检测AdRotator V2.1是否显示广告或发生错误

时间:2015-03-16 01:07:03

标签: windows-store-apps windows-8.1 ads adrotator

我想根据展示广告或发生错误的情况更新我的用户界面。我正在使用AdRotator v2.1。查看源代码,如果无法从各种提供程序提供广告,则控件将崩溃,并且IsAdRotatorEnabled将设置为false。但该属性不会触发通知更改。如何检测是否没有展示广告?

Enabled="{Binding AreAdsEnabled,Mode=TwoWay,FallbackValue=true,UpdateSourceTrigger=PropertyChanged}"

1 个答案:

答案 0 :(得分:0)

这是我正在使用的黑客。它非常脆弱。它查看字符串的日志消息,以检测是否发生了错误。

public class BaseAdControl
{
    public AdRotatorControl CurrentAdRotatorControl { get; set; }
    private UserControl userControlWrapper;


    public BaseAdControl(AdRotatorControl MyAdRotatorControl, UserControl userControlWrapper)
    {
        // TODO: Complete member initialization
        this.CurrentAdRotatorControl = MyAdRotatorControl;
        this.userControlWrapper = userControlWrapper;


        MyAdRotatorControl.PlatformAdProviderComponents.Add(AdRotator.Model.AdType.PubCenter, typeof(Microsoft.Advertising.WinRT.UI.AdControl));
        MyAdRotatorControl.PlatformAdProviderComponents.Add(AdRotator.Model.AdType.AdDuplex, typeof(AdDuplex.Universal.Controls.Win.XAML.AdControl));


        MyAdRotatorControl.Loaded += MyAdRotatorControl_Loaded;
        MyAdRotatorControl.Unloaded += MyAdRotatorControl_Unloaded;
    }
    #region Public Properties



    #endregion Public Properties

    #region Public Methods

    public virtual void adDuplex_AdLoadingError(object sender, AdDuplex.Universal.Win.WinRT.Models.AdLoadingErrorEventArgs e)
    {
        AdDuplex.Universal.Controls.Win.XAML.AdControl adCtrl = sender as AdDuplex.Universal.Controls.Win.XAML.AdControl;
        adCtrl.AdLoadingError -= adDuplex_AdLoadingError;
        Utilities.logger.LogDebug(e.Error.Message);
        this.userControlWrapper.Visibility = Visibility.Collapsed;
        Utilities.logger.LogDebug("Updated Visibility to: " + this.userControlWrapper.Visibility);
    }

    public virtual async void Logger(string message)
    {
        Utilities.logger.LogDebug("AdRotator: " + message);
        if (string.Equals(message, "No Ads available", StringComparison.CurrentCultureIgnoreCase))
        {

            this.userControlWrapper.Visibility = Visibility.Collapsed;
            Utilities.logger.LogDebug("Updated Visibility to: " + this.userControlWrapper.Visibility);
        }
        else if (message.Contains("Ad created for provider"))
        {
            var cont = CurrentAdRotatorControl as Control;
            Object adType = null;
            if (cont != null)
            {
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                       () =>
                       {
                           Border border = VisualTreeHelper.GetChild(CurrentAdRotatorControl, 0) as Border;
                           if (border != null)
                           {
                               adType = border.Child;
                           }
                       });
                if (adType != null)
                {
                    if (adType is Microsoft.Advertising.WinRT.UI.AdControl)
                    {
                        var pubsub = adType as Microsoft.Advertising.WinRT.UI.AdControl;
                        if (pubsub != null)
                            pubsub.ErrorOccurred += pubsub_ErrorOccurred;
                    }
                    else if (adType is AdDuplex.Universal.Controls.Win.XAML.AdControl)
                    {
                        var adDuplex = adType as AdDuplex.Universal.Controls.Win.XAML.AdControl;
                        if (adDuplex != null)
                            adDuplex.AdLoadingError += adDuplex_AdLoadingError;
                    }
                    else if (adType is SomaAd)
                    {
                        var smato = adType as SomaAd;
                        if (smato != null)
                            smato.GetAdError += smato_GetAdError;
                    }
                }
            }

            this.userControlWrapper.Visibility = Utilities.AreAdsEnabled ? Visibility.Visible : Visibility.Collapsed;
            Utilities.logger.LogDebug("Updated Visibility to: "+this.userControlWrapper.Visibility);
        }
    }

    public virtual void MyAdRotatorControl_Loaded(object sender, RoutedEventArgs e)
    {
        AdRotatorControl.Log += Logger;
    }

    public virtual void MyAdRotatorControl_Unloaded(object sender, RoutedEventArgs e)
    {
        AdRotatorControl.Log -= Logger;
    }

    public virtual void pubsub_ErrorOccurred(object sender, Microsoft.Advertising.WinRT.UI.AdErrorEventArgs e)
    {
        Microsoft.Advertising.WinRT.UI.AdControl adCtrl = sender as Microsoft.Advertising.WinRT.UI.AdControl;
        adCtrl.ErrorOccurred -= pubsub_ErrorOccurred;
        Utilities.logger.LogDebug(e.Error + " ," + e.ErrorCode);
        this.userControlWrapper.Visibility = Visibility.Collapsed;
        Utilities.logger.LogDebug("Updated Visibility to: " + this.userControlWrapper.Visibility);
    }

    public virtual void smato_GetAdError(object sender, string ErrorCode, string ErrorDescription)
    {
        SomaAd adCtrl = sender as SomaAd;
        adCtrl.GetAdError -= smato_GetAdError;
        Utilities.logger.LogDebug(ErrorDescription + " ," + ErrorCode);
        this.userControlWrapper.Visibility = Visibility.Collapsed;
        Utilities.logger.LogDebug("Updated Visibility to: " + this.userControlWrapper.Visibility);
    }

    #endregion Public Methods
}