wp7appdevelopment

Adventures in Windows Phone app development

Monthly Archives: May 2012

Displaying Your Ads as a Fallback for Microsoft pubCenter

Microsoft pubCenter currently seems to be the best choice for monetizing apps on the Windows Phone. But all too often, pubCenter’s inventory is too low and it fails to serve an ad for your app.  You can detect when an error occurs displaying an ad, and display your ad banner instead.  This way, your app will display pubCenter ads when it can; and when it can’t, it will fall back to displaying your ad.

The key is to create a banner that’s the same size as the pubCenter ad control, and set its visibility to Collapsed. In the code behind, you need to add event handlers for ErrorOccurred and AdRefreshed. In the ErrorOccurred event handler, which is called when pubCenter is unable to serve an ad, you’ll collapse the Microsoft ad control and set your ad to visible. In the AdRefreshed event handler, which is called when pubCenter successfully servers an ad, you’ll do the opposite: collapse your ad and set the Microsoft ad control to visible.

In the Xaml

You can place both controls in a StackPanel. Or, place in the same location, like the same Grid Row/Column. Just be sure to collapse your control.

    xmlns:my="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
    <my:AdControl Name="adControl1" Grid.Row="0" Height="80" Width="480"
        HorizontalAlignment="Left" Margin="0"  VerticalAlignment="Top"
        AdUnitId="YOUR_ID" ApplicationId="YOUR_APP_ID"/>
    <Border Name="MyAdBorder" Grid.Row="0" Height="80" Width="480" VerticalAlignment="Top"
        Background="WhiteSmoke" Visibility="Collapsed">
        <!-- whatever you want to display in your ad -->
    </Border>

In the Code Behind

Modify your class constructor, and attach the event handlers to pubCenter control events. Depending on the result of the pubCenter request, either your ad or the pubCenter ad control is collapsed and the other one is displayed.

    using Microsoft.Advertising.Mobile;
    public MainPage()
    {
        InitializeComponent();

         adControl1.ErrorOccurred += new EventHandler<Microsoft.Advertising.AdErrorEventArgs>(adControl1_ErrorOccurred);
         adControl1.AdRefreshed += new EventHandler(adControl1_AdRefreshed);
    }

        // An ad was successfully displayed; hide our advertising banner
        void adControl1_AdRefreshed(object sender, EventArgs e)
        {
            MyAdBorder.Visibility = System.Windows.Visibility.Collapsed;
            adControl1.Visibility = System.Windows.Visibility.Visible;
        }

        // error displaying AdCenter ad; show our advertising banner
        void adControl1_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
        {
            MyAdBorder.Visibility = System.Windows.Visibility.Visible;
            adControl1.Visibility = System.Windows.Visibility.Collapsed;
        }

NOTE: pubCenter visually hides the ad control when there’s no ad to display. But relying on this alone won’t work! Even though nothing displays, it still occupies the space and catches all taps, making any controls in your ad unclickable. The key is to explicitly set the control’s visual state to Collapsed.