首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检测AdRotator V2.1是否显示广告或出现错误

如何检测AdRotator V2.1是否显示广告或出现错误
EN

Stack Overflow用户
提问于 2015-03-16 01:07:02
回答 1查看 137关注 0票数 0

我想更新我的UI根据场景,当一个广告正在显示或错误发生。我使用的是AdRotator v2.1。从源代码来看,如果控件不能为来自不同提供者的广告提供服务,那么该控件就会崩溃,并且IsAdRotatorEnabled将被设置为false。但该属性不会触发通知更改。如果没有广告,我如何检测?

代码语言:javascript
复制
Enabled="{Binding AreAdsEnabled,Mode=TwoWay,FallbackValue=true,UpdateSourceTrigger=PropertyChanged}"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-28 00:45:12

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

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29068190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档