Using Microsoft Advertising Control in WP7 apps
This article explains how to monetize Windows Phone 7 application using Microsoft Advertising Ad control.
Article Metadata
Code Example
Tested with
Article
Introduction
In mobile application development the term monetization is used to mean the ability to generate revenue thorough your mobile application. It can be from affiliate programs, e- commerce, premium content and advertising.
The market for Windows Phone 7 applications is wide open for developers wanting to monetize their mobile applications. That means endless opportunities for any developer looking to monetize mobile applications. One way of monetization can be advertisement i.e. displaying ads in your apps. In this article we'll see how to achieve such kind of functionality using Microsoft Advertising Ad Rotator Control.
Implementation
Following are the basic steps to be implemented in a Windows Phone 7 application.
Step-1: Download Microsoft Advertising SDK.
Download Microsoft Advertising SDK for Windows Phone. After download Microsoft Advertising SDK, install it on your development machine.
Step-2: Register mobile App at Microsoft pubCenter.
Register Your Mobile App: Sign up and register your Windows Phone apps using Microsoft pubCenter. Here you can use your live or pubCenter Id.
Register your first application and create an ad unit (optional).
Enter required information to register your application.
Enter application name:
Device type:
Ad unit name:
Ad unit size:
Select Ad category:
Excluded url:
Step-3: Creating the Windows Phone application
1) Create a Windows Phone 7 Application project
2) Right click on project in solution explorer panel and click on Add Reference dialog box, click the Browse tab. Navigate to the location where you installed the Microsoft.Advertising.Mobile.UI.dll previously. Click the Microsoft.Advertising.Mobile.UI.dll to select it, and click OK.
3) Now configure Application Id and Ad Unit Id properties in the Ad Control. Here is two ways to add “Ad Control” in the application using XAML and C# code behind.
Using XAML Drag and drop the Ad Control to Windows Phone 7 xaml page and configure Application Id and Ad Unit Id properties in xaml. See below code snippet:
<my:AdControl Foreground="Red" AdUnitId="AdUnitId" ApplicationId="AppId" Height="80" HorizontalAlignment="Left" Margin="0,6,0,0" Name="adControl1" VerticalAlignment="Top" Width="480" IsAutoRefreshEnabled="True" />Using (C#) code behind Double click on the Windows Phone7 Page, it will automatically open the code behind of a page. And using the AdControl API which is a managed api. Include the Microsoft.Advertising.Mobile.UI namespace in the project. And create an instance of the AdControl Class and set the properties of the AdControl class instance or you can directly pass it to AdControl constructor. See below code snippet:
private const string APPLICATION_ID = "";
private const string AD_UNIT_ID = "";
private AdControl adControl;
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
this.Unloaded += new RoutedEventHandler(MainPage_Unloaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(APPLICATION_ID) && !string.IsNullOrEmpty(AD_UNIT_ID))
{
adControl = new AdControl(
APPLICATION_ID,
AD_UNIT_ID,
true);
// Make the AdControl size large enough that it can contain the image
adControl.Width = 480;
adControl.Height = 80;
adControl.Keywords = "Windows Phone 7, Hotels, Travels, Gold";
ContentPanel.Children.Add(adControl);
}
else
{
MessageBox.Show("To show ads in this application, insert the application ID and an ad unit ID into your code as explained..");
}
}
void MainPage_Unloaded(object sender, RoutedEventArgs e)
{
ContentPanel.Children.Remove(adControl);
}
Run the application by using F5 button, and you will see a sample of Monetization in Windows Phone 7 Application.
Example source code
- Source code of an example app can be downloaded from here - File:WP AdApp.zip








Contents
Croozeus - Renamed and subedited
Hi Pavan,
Good article! Using Ads can be so easy in WP7 :)
I sub-edited the article and moved it to the new title. New title just because monetization may include several options while this article focuses on one of them i.e. advertising.
//Pcroozeus 09:00, 1 February 2012 (EET)
Pavan.pareta -
Good observation and addressed it.
Thank you Croozeus !
//ppPavan Pareta 12:46, 1 February 2012 (EET)
Hamishwillee - Also added categories and see also
Hi
Added In-App advertising category to make this easier to find.
Added SeeAlso with links to main MSDN topics about advertising. Feel free to add more.
Regards
Hhamishwillee 04:51, 24 April 2012 (EEST)
Amol Tembhukar - Not Showing Control When Run on the emulator
Hi,
I have created the same app with your ref code. But, While running the app on emulator its not showing the Ad Control on the screen.
Waiting for reply.
Regards,
- Amol TAmol_Tembhukar 09:11, 7 June 2012 (EEST)
Pavan.pareta - RE: Not Showing Control When Run on the emulator
Hello Amol,
Make sure that you have Internet Connectivity on your computer, however you can also try this code on wp7 device as well.
//PPPavan Pareta 05:20, 14 June 2012 (EEST)