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

How to detect If AdRotator V2.1 is showing an Ad or an error occurred

我想根据正在显示广告或发生错误的情况更新我的 UI。我正在使用 AdRotator v2.1。查看源代码,如果控件无法提供来自不同提供商的广告,它似乎会崩溃,并且 IsAdRotatorEnabled 将设置为 false。但是 属性 不会触发通知更改。我如何检测是否没有显示广告?

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

这是我目前正在使用的 hack。它很脆。它正在查看字符串的日志消息以检测是否发生了错误。

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
}