Adding AdRotator using Attached Behavior

Jun 3, 2013 at 9:38 PM
Edited Jun 3, 2013 at 9:41 PM
I attached the pub Center controls to my app using a simple attached behavior that I drop onto a Grid in my app.

The pubCenter ad worked great. However, I am running into issues on my development devices with the adRotator control. I'm not sure if it is because I am testing on a simulator / dev device.

Here is the code I use in my attached behavior:
public class AdBehavior : Behavior<Grid>
    {
        AdRotatorControl adRotator = null;

        protected override void OnAttached()
        {
            base.OnAttached();

            if (this.AssociatedObject is Grid)
            {
                adRotator = new AdRotatorControl();
                var grid = this.AssociatedObject as Grid;
                grid.Loaded += (s, e) =>
                {
                    adRotator.Invalidate();
                };
                adRotator.Width = 480;
                adRotator.Height = 80;
                adRotator.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                adRotator.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                adRotator.PubCenterAdUnitId = SessionContext.Ads.HorizontalAdUnit;
                adRotator.PubCenterAppId = SessionContext.Ads.ApplicationIdentifier;
                adRotator.SettingsUrl = SessionContext.Ads.RemoteConfigUrl;
                adRotator.Log += adRotator_Log;

                grid.Children.Add(adRotator);


                //var adControl = new AdControl();
                //adControl.ApplicationId = SessionContext.Ads.ApplicationIdentifier;
                //adControl.AdUnitId = SessionContext.Ads.HorizontalAdUnit;
                //adControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                //adControl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                //adControl.Height = 80;
                //adControl.Width = 480;
                //adControl.IsAutoRefreshEnabled = true;
                //adControl.IsAutoCollapseEnabled = false;
                //adControl.AdRefreshed += adControl_AdRefreshed;
                //adControl.ErrorOccurred += adControl_ErrorOccurred;
                //grid.Children.Add(adControl);
            }
        }

        void adRotator_Log(string message)
        {
            
        }

        void adControl_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
        {
            
        }

        void adControl_AdRefreshed(object sender, EventArgs e)
        {
            
        }
    }
I get the following log messages:
  1. Ads are enabled for display
  2. Ads being requested for: None
  3. No ads available
Here is my remote file:
<?xml version="1.0" encoding="utf-8"?>
<AdSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <CultureDescriptors>
        <AdCultureDescriptor CultureName="en-US">
      <Probabilities Probability="30" AdType="AdMob" AppID="xxx" />
      <Probabilities Probability="60" AdType="PubCenter" AppID="xxx" SecondaryID="xxx" />
      <Probabilities Probability="10" AdType="AdDuplex" AppID="xxx"/>
      <Probabilities AdType="DefaultHouseAd"/>
    </AdCultureDescriptor>
        <AdCultureDescriptor CultureName="en-GB">
      <Probabilities Probability="30" AdType="AdMob" AppID="xxx" />
      <Probabilities Probability="60" AdType="PubCenter" AppID="xxx" SecondaryID="xxx" />
      <Probabilities Probability="10" AdType="AdDuplex" AppID="xxx"/>
      <Probabilities AdType="DefaultHouseAd"/>
    </AdCultureDescriptor>
        <AdCultureDescriptor CultureName="de-DE">
      <Probabilities Probability="30" AdType="AdMob" AppID="xxx" />
      <Probabilities Probability="60" AdType="PubCenter" AppID="xxx" SecondaryID="xxx" />
      <Probabilities Probability="10" AdType="AdDuplex" AppID="xxx"/>
      <Probabilities AdType="DefaultHouseAd"/>
    </AdCultureDescriptor>
        <AdCultureDescriptor CultureName="default">
      <Probabilities Probability="30" AdType="AdMob" AppID="xxx" />
      <Probabilities Probability="60" AdType="PubCenter" AppID="xxx" SecondaryID="xxx" />
      <Probabilities Probability="10" AdType="AdDuplex" AppID="xxx"/>
      <Probabilities AdType="DefaultHouseAd"/>
    </AdCultureDescriptor>
    </CultureDescriptors>
</AdSettings>
Jun 4, 2013 at 5:50 PM
Ok first off, don't use ID's in two places. Either set them in your config file or in code, not BOTH.

That is certainly an innovative use of the control.

not able to see what's goign wrong there from looking at your snippet, so I'd suggest downloading the source for AdRotator, adding the project to your own and referencing that.

Then step through from the invalidate function to see why it's either:
  • Not able to download your config (check validdescriptors has values in the GetConfig Culture method)
  • Not able to instantiate the selected ad provider
let me know if this helps