Export (0) Print
Expand All

XAML - Windows 8.1

Microsoft Advertising Services

Updated: April 30, 2014

Published: April 30, 2014

The following walkthrough shows how to include advertising in a Windows 8.1 app written with a XAML front end and .NET code-behind. It assumes that you’ve already written an app or are starting from a sample Windows 8.1 app or other built-in Visual Studio template.

Before You Begin

  • Before you can use the SDK, you must have Windows 8.1, a version of Visual Studio 2013 for Windows 8.1, and the Advertising SDK installed on your system. For more information, see Getting Started.

Walkthrough

  1. From the Solution Explorer window, double-click the “Package.appxmanifest” file.


  2. Select the Capabilities tab, and ensure that the Internet Client check box is selected. Without this capability, the ad client cannot make calls to the Internet to fetch ads.

    Package.appxmanifest


  3. From the Solution Explorer window, right click References, and then select Add Reference….


  4. In Reference Manager, click Windows, and that will expand to show Extensions. Click Extensions, select Microsoft Advertising SDK for Windows 8.1 (XAML), and then click OK.


  5. Confirm that your reference was added. Your Solution Explorer should look similar to the following:


  6. Modify the XAML for the page where you are embedding advertising to include the Microsoft.Advertising.WinRT.UI namespace. For example, in the default sample app generated by Visual Studio (named, in this app, My Ad Funded Windows 8.1 XAML App), the XAML page is MainPage.XAML.

    The Page section of the MainPage.XAML file generated by Visual Studio has the following code:

    <Page
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:My_Windows_8._1_Ad_Funded_XAML_App"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:UI="using:Microsoft.Advertising.WinRT.UI"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    
        </Grid>
    </Page>
    
  7. In the Grid tag, add the code for the AdControl. The complete Grid tag looks like this code.

    
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    
            <UI:AdControl ApplicationId="d25517cb-12d4-4699-8bdc-52040c712cab" 
                          AdUnitId="10043107" 
                          HorizontalAlignment="Left" 
                          Height="250" 
                          VerticalAlignment="Top" 
                          Width="250"/>
    </Grid>
    

    The complete code for the MainPage.XAML file should look like

    <Page
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:My_Windows_8._1_Ad_Funded_XAML_App"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:UI="using:Microsoft.Advertising.WinRT.UI"
        x:Class="My_Windows_8._1_Ad_Funded_XAML_App.MainPage"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    
            <UI:AdControl ApplicationId="d25517cb-12d4-4699-8bdc-52040c712cab" 
                          AdUnitId="10043107" 
                          HorizontalAlignment="Left" 
                          Height="250" 
                          VerticalAlignment="Top" 
                          Width="250"/>
        </Grid>
    </Page>
    
    
  8. Compile and run the app to see it with an ad, similar to the following:

    Ad in Tablet Emulator


Next Steps

To see how different ad types render in the sample app, change the AdUnitId property to any of the values listed in the TestMode topic. Change the Height and Width properties in the XAML code (above) to match the ad size you choose. The AdControl must be the same size as the ad unit or larger.

C#: See the topic XAML Markup with All Properties for an example of how to assign event handlers to AdControl events. Then see the topic C# Event Handlers for sample code that shows event handlers written in C#.

Visual Basic: See the topic XAML Markup with All Properties for an example of how to assign event handlers to AdControl events. Then see the topic Visual Basic Event Handlers for sample code that shows event handlers written in VB.

C++: The current Ad SDK does support C++. The AdControl loads the CLR and uses managed C++. Fully native support is planned for a future release -- there is no release ETA.

Error Handling: To learn about how to handle errors in AdControl code, see the topic AdControl Error Handling.

More: See the API Reference section for the properties, methods and events of the AdControl.

Support and Feedback

Get product support or report a bug in the advertising SDK by visiting the Windows Store Apps developer support page support page. For product support, under "App development" click the "Advertising SDK" link. To post a question in the support forum, scroll down to "Find answers online" and click "Ads-in-Apps for Windows 8 forum".

Send feedback about this documentation to adsfdbk@microsoft.com. There is no product support through this alias. Documentation feedback only.

Community Additions

ADD
Show:
© 2014 Microsoft