Share via


Banner ads in Windows Phone Silverlight apps

[ This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation ]

This walkthrough shows how to use the AdControl class to display banner ads in a Silverlight app for Windows Phone 8.1 or Windows Phone 8.0.

Advertising support for Windows Phone 8.x Silverlight projects

Some developer scenarios are no longer supported in Windows Phone 8.x Silverlight projects. For more information, see the following table.

Platform version Existing projects New projects
Windows Phone 8.0 Silverlight If you have an existing Windows Phone 8.0 Silverlight project that already uses an AdControl and this app is already published in the Windows Store, you can modify and rebuild the project, and you can debug or test your changes on a device. Debugging or testing the project in the emulator is not supported. Not supported
Windows Phone 8.1 Silverlight If you have an existing Windows Phone 8.1 Silverlight project that uses an AdControl, you can modify and rebuild the project. However, to debug or test the app, you must run the app in the emulator and use test values for the application ID and ad unit ID. Debugging or testing the app on a device is not supported. You can add an AdControl to a new Windows Phone 8.1 Silverlight project. However, to debug or test the app, you must run the app in the emulator and use test values for the application ID and ad unit ID. Debugging or testing the app on a device is not supported.

 

Add the advertising assemblies to your project

To get started, download and install the NuGet package that contains the Microsoft advertising assemblies for Windows Phone Silverlight to your project.

  1. Open your project in Visual Studio.

  2. Click Tools, point to NuGet Package Manager, and click Package Manager Console.

  3. In the Package Manager Console window, enter one of these commands.

    • If your project targets Windows Phone 8.0, enter this command.

      Install-Package Microsoft.Advertising.WindowsPhone.SL80 -Version 6.2.40501.1
      
    • If your project targets Windows Phone 8.1, enter this command.

      Install-Package Microsoft.Advertising.WindowsPhone.SL81 -Version 8.1.50112
      

    After entering the command, all the necessary assemblies for Silverlight are downloaded to your local project via NuGet packages, and references to these assemblies are automatically added to your project.

Code development

  1. Add the following capabilities to the in the Capabilities node in your WMAppManifest.xml file.

      <Capability Name="ID_CAP_IDENTITY_USER"/>
      <Capability Name="ID_CAP_MEDIALIB_PHOTO"/>
      <Capability Name="ID_CAP_PHONEDIALER"/>
    
  2. Add the Internet (Client & Server) capability to the Package.appxmanifest file in your project.

  3. Save your project.

  4. Modify the Silverlight markup in the MainPage.xaml file to include the Microsoft.Advertising.Mobile.UI namespace.

    xmlns:UI="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
    
  5. In the Grid tag, add the following code for the AdControl. Assign the AdUnitId and ApplicationId properties to the test values provided in Set up ad units in your app. Also adjust the height and width of the control so it is one of the supported ad sizes for banner ads (provided in Banner ads).

    Note  Every AdControl has a corresponding ad unit that is used by our services to serve ads to the control, and every ad unit consists of an ad unit ID and application ID. In these steps, you assign test ad unit ID and application ID values to your control. These test values can only be used in a test version of your app. Before you publish your app to the Store, you must replace these test values with live values from Windows Dev Center.

     

    <Grid x:Name="ContentPanel" Grid.Row="1">
       <UI:AdControl
          ApplicationId="3f83fe91-d6be-434d-a0ae-7351c5a997f1"
          AdUnitId="10865270"
          HorizontalAlignment="Left"
          Height="80"
          VerticalAlignment="Top"
          Width="480"/>
    </Grid>
    
  6. Compile and run the app to see it with a test ad.

Release your app with live ads using Windows Dev Center

  1. In the Dev Center dashboard, go to the Monetize with ads page for your app and create an ad unit. For the ad unit type, specify Banner. Make note of both the ad unit ID and the application ID.
  2. In your code, replace the test ad unit values (ApplicationId and AdUnitId) with the live values you generated in Dev Center.
  3. Submit your app to the Store using the Dev Center dashboard.
  4. Review your advertising performance reports in the Dev Center dashboard.

Banner ads

Interstitial ads

Set up ad units in your app