Using the Silverlight API for Windows Phone
The following steps illustrate how to use C# code and the Windows Phone ad control to show ads in apps.
Prerequisites
-
Microsoft Visual Studio 2010 (or Microsoft Visual Studio 2010 Express)
-
Microsoft Advertising Ad Control SDK for Windows Phone
Follow Steps 1 through 7 in the topic How to: Create Your First Silverlight Application for Windows Phone
Important The purpose of each code sample provided in this documentation is to illustrate a concept and related syntax. Samples do not include all the code that is found in a full production system. For example, data validation and error handling code is not included. Technical support is not available for these samples or for any included source code. A complete working app, with code for using geo location ad targeting, margin thickness, and the events
AdRefreshedandErrorOccurredis available for download at code.microsoft.com.
Integrate an AdControl Programmatically
-
If you haven’t done so already as part of the Prerequisites, above, follow Steps 1 through 7 in the topic How to: Create Your First Silverlight Application for Windows Phone.
-
In Visual Studio Solution Explorer, right-click References under your project and click Add Reference….
-
In the Add Reference dialog box, click the .NET tab. Click Microsoft.Advertising.Mobile.UI.dll entry to select it, and click OK.
-
In the Solution Explorer panel, you will see the Microsoft.Advertising.Mobile.UI.dll listed in the References folder of your project.
-
In the source file that you want to use to program the AdControl, add the following code in the
usingsection of your code.using Microsoft.Advertising.Mobile.UI; -
In your code, you will create an instance of the
AdControland set its properties. This example uses the default main page of the project MainPage.xaml.cs. The following code creates theadControlobject in theMainPageconstructor.// Constructor public MainPage() { InitializeComponent(); // ApplicationID = "Test_client", AdUnitID = "Image480_80", AdControl adControl = new AdControl("test_client", // ApplicationID "Image480_80", // AdUnitID true); // isAutoRefreshEnabled // Make the AdControl size large enough that it can contain the image adControl.Width = 480; adControl.Height = 80; } -
Add the
adControlobject to your application page layout. In the following code, the application page layout has two child elements: aStackPanel(this.LayoutRoot.Children[0]) and aGrid(this.LayoutRoot.Children[1]). The following code makes theadControlobject a child of theGrid.Grid grid = (Grid)this.LayoutRoot.Children[1]; grid.Children.Add(adControl);
Your entire MainPage() code will look like this.
// Constructor public MainPage() { InitializeComponent(); // ApplicationID = "Test_client", AdUnitID = "Image480_80", AdControl adControl = new AdControl("test_client", // ApplicationID "Image480_80", // AdUnitID true); // isAutoRefreshEnabled // Make the AdControl size large enough that it can contain the image adControl.Width = 480; adControl.Height = 80; Grid grid = (Grid)this.LayoutRoot.Children[1]; grid.Children.Add(adControl); } -
Run your application and you will see a sample test ad.
See Also
Send feedback about this documentation to adsfdbk@microsoft.com.
Publication date: 2012-03-09
- 11/14/2011
- Ron Grant - MSFT
In order to handle the situation where the page is unloaded while an ad is still being fetched you need to remove the control from the page doing the form unload.
void myForm_Unloaded(object sender, RoutedEventArgs e)
{
// I named the placeholder for the adcontrol a grid named AdPanel to make it easy to find
Grid grid = (Grid)this.LayoutRoot.FindName("AdPanel");
if (grid != null)
{
if (grid.Children.Contains(adControl))
{
grid.Children.Remove(adControl);
grid.Visibility = System.Windows.Visibility.Collapsed;
}
}
this.Unloaded -= new RoutedEventHandler(ParkDisplay_Unloaded);
adControl = null;
}
- 8/25/2011
- J Short