Set up ads in Windows Phone 8

ellemayo picture ellemayo · Nov 28, 2012 · Viewed 8.5k times · Source

I found a few scattered responses to similar problems (mostly with Windows Phone 7) but wanted to post an answer that contained all requirements for Windows Phone 8 Ads.

I've been having some issues setting an ad up in Windows Phone 8. Every time I ran the emulator I ran into an issue where the app would run but the ad would not show up.

The app ran properly but I noticed in the output console that the following exception was logged:

An exception of type 'Microsoft.Advertising.Shared.AdException' occurred in Microsoft.Advertising.Mobile.DLL and wasn't handled before a managed/native boundary

Ads show up fine in one app I am developing but not another and the exception does not show any helpful information.

Answer

ellemayo picture ellemayo · Nov 28, 2012

I figured out how to catch the exception and see what the issue actually was (which was a large amount of missing capabilities in my Manifest file). In order to try and catch the exception from the AdControl and get the data I needed, I added the following to my page.

Catch Ad Control Errors

    public MainPage()
    {
        InitializeComponent();

        AdUnit.ErrorOccurred += AdUnit_ErrorOccurred;
    }

    void AdUnit_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
    {
        throw new NotImplementedException();
    }

Which showed me that I was missing the MEDIALIB permission and the PHONEDIALER permission. I ran some testing and determined that any app that runs ads will require the following permissions:

Required Permissions

ID_CAP_IDENTIFY_USER
ID_CAP_MEDIALIB_PHOTO
ID_CAP_NETWORKING
ID_CAP_PHONEDIALER
ID_CAP_WEBBROWSERCOMPONENT

Set Parameters

From other posts I've read it is also important to make sure you set your ad units width/height properly to 480/80, if it is auto and doesn't have the minimum demensions then the ad will not show.

Make sure that to view it in the emulator you can only use test ApplicationId and AdUnitId. PubCenter credentials will only work in a published application.

<UI:AdControl x:Name="AdUnit" Height="80" Width="480"
    AdUnitId="Image480_80" ApplicationId="test_client" />

Hide or Swap Failed/Empty Ad Controls

If your AdControl fails it leaves a big empty space in your ad. You can either hide it or swap it with an ad from another network. To do this, catch the exception as shown above (AdUnit_ErrorOccurred) and added the following:

To Hide:

AdUnit.Height = 0;
AdUnit.Visibility = System.Windows.Visibility.Collapsed;

Setting the visibility didn't work on its own, the height has to be set to 0 as well.

Swap Ad:

Instead of hiding the AdControl, you could show an alternate ad bar from a service like http://www.adduplex.com.