Using display ads in Windows Phone 7 apps

Enabling display ads in a Windows Phone 7 app is a three-step process:

  1. Installing the Microsoft Advertising SDK.
  2. Registering the application with the Microsoft pubCenter.
  3. Embedding the ad control in the application.

Installing the SDK

The Microsoft Advertising SDK is available on the Microsoft Advertising site. The SDK is an .msi installer file, and executing it copies several .dlls and help files to the program files directory. On my PC the files are located in:

C:\Program Files (x86)\Microsoft Advertising SDK for Windows Phone 7

Registering the app

There are two main goals in the registration process:

  1. Subscribing your app to several ad channels that will be relevant to the app’s users.
  2. Obtaining unique ad unit and application IDs. These ties your app to the channels you select and are used to track the number of impressions your app generates.

To register your app:

  1. Visit the Microsoft Advertising pubCenter and either create a new account or use your existing Microsoft Live account to sign in.
  2. Click Setup, then Register. Enter your app’s name and click Save. Your app will appear under the Sites & apps tab. Notice the application ID; you’ll need it later.
  3. Click Ad unit, then select Create a new application ad unit.
  4. Enter a name for the ad unit, then select up to three categories. These determine the types of ads your users will see, so choose categories that seem relevant.
  5. Click Save. Note the ad unit ID; you’ll need this one later as well.

Embedding the ad control

Before you can implement the ad control in your app, you need a reference to Microsoft.Advertising.Mobile.UI, which you can add by right-clicking on the References folder in the Solution Explorer, then clicking Add Reference. Browse to the advertising SDK folder and select the Silverlight .dll.

Open the XAML page that will hold your ad control, and add an XML namespace for the advertising assembly:

<phone:PhoneApplicationPage x:Class="Wp7AdControlSetup.MainPage"

Next, insert the ad control itself into your XAML page:

<advertising:AdControl ApplicationId="test_client"

The ad control is now installed and set up to run in test mode.

Test mode vs. production mode

When the ad control is configured for test mode, you should see something like this when you run the app in the emulator:

Test ads

Sometimes the ads take a few seconds to start loading, but if you don’t see anything after a minute or two, something is set up incorrectly.

You can also view live ads in the emulator and on your unlocked device before publishing your app to the Marketplace. Production ads look like this:

Live ads

Switching the ad control to show live ads involves adding production ad and unit IDs to the control’s XAML and disabling test mode for your application. First, add the app and ad unit IDs that you received during the pubCenter registration process to the advertising control:

<advertising:AdControl ApplicationId="3a6a7b1b-3b66-4fa0-9732-514fc9aa8b89"
                       AdModel="Contextual" />

Next, open up the App.xaml.cs file and add the following code in the public App() constructor, beneath the InitializeComponent() method:

public App()

    AdControl.TestMode = false;

Failing to set the AdControl.TestMode early enough in the application’s lifecycle will cause the ad control to load an empty frame. Setting it in the application’s constructor ensures that it is set before the control itself is rendered.

If you want to revert to test ads, reinsert the test app and ad unit IDs and remove the line that sets AdControl.TestMode = false.

Download the sample

A full working example is available on my CodePlex site. The sample includes real ad unit and application IDs so you can see live ads in the emulator. It was built with the February 2011 version of the Windows Phone developer tools. A reference to the advertising .dll is included, but you will probably need to relink the .dll to your local copy of the advertising SDK.


1 Comment

Filed under Windows Phone 7

One response to “Using display ads in Windows Phone 7 apps

  1. Ian

    NB If you are outside the USA you won’t have access to the Microsoft Ad SDK – there are some alternatives here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s