问题
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}"
回答1:
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
}
来源:https://stackoverflow.com/questions/29068190/how-to-detect-if-adrotator-v2-1-is-showing-an-ad-or-an-error-occurred