2 out of 2 rated this helpful - Rate this topic

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

  1. Microsoft Visual Studio 2010 (or Microsoft Visual Studio 2010 Express)

  2. Windows Phone Developer Tools

  3. 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

    ImportantImportant
    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 AdRefreshed and ErrorOccurred is available for download at code.microsoft.com.

Integrate an AdControl Programmatically

  1. 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.

  2. In Visual Studio Solution Explorer, right-click References under your project and click Add Reference….

    Right click “Reference” under your project
  3. In the Add Reference dialog box, click the .NET tab. Click Microsoft.Advertising.Mobile.UI.dll entry to select it, and click OK.

    In “Add Reference”, choose the tab “.NET”
  4. In the Solution Explorer panel, you will see the Microsoft.Advertising.Mobile.UI.dll listed in the References folder of your project.

    see under the “Reference” folder of your project
  5. In the source file that you want to use to program the AdControl, add the following code in the using section of your code.

    using Microsoft.Advertising.Mobile.UI;

  6. In your code, you will create an instance of the AdControl and set its properties. This example uses the default main page of the project MainPage.xaml.cs. The following code creates the adControl object in the MainPage constructor.

    // 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;
    }
    
    
  7. Add the adControl object to your application page layout. In the following code, the application page layout has two child elements: a StackPanel (this.LayoutRoot.Children[0]) and a Grid (this.LayoutRoot.Children[1]). The following code makes the adControl object a child of the Grid.

    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);
            }
    
    
  8. Run your application and you will see a sample test ad.

    Run your code and the AdControl shows

See Also


Send feedback about this documentation to adsfdbk@microsoft.com.
Publication date: 2012-03-09
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Install the November Update to the Advertising SDK
The November update of the Advertising SDK implements better exception handling for the circumstance JShort describes. Download and install from http://www.microsoft.com/download/en/details.aspx?id=8729.
Exceptions from ad control

    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;
}