如何检测AdRotator V2.1是否显示广告或发生错误 [英] How to detect If AdRotator V2.1 is showing an Ad or an error occurred

查看:86
本文介绍了如何检测AdRotator V2.1是否显示广告或发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to update my UI based on the scenario when an Ad is being shown or an error occurred. I am using AdRotator v2.1. Looking at the source code it seems that the control would collapse if it could not serve an ad from various provider, and the IsAdRotatorEnabled would be set to false. But that property does not trigger an notification change. How can i detect if no ads are being shown?

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

推荐答案

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

This is hack i am currently using. Its quite brittle. Its looking at log message for strings to detect if an error has occurred.

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
}

这篇关于如何检测AdRotator V2.1是否显示广告或发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆