Export (0) Print
Expand All

XAML (.NET) - Windows 8.0

Microsoft Advertising Services

Updated: August 2, 2013

Published: June 4, 2013

The following walkthrough shows how to include advertising in a Windows 8 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 app or other built-in Visual Studio template.

Before You Begin

  • Before you can use the SDK, you must have Windows 8, a version of Visual Studio 2012 for Windows 8, 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.

    Solution Explorer


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

    Solution Explorer References


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

    Reference Manager Browse


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

    Solution Explorer Confirm References


  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 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_Ad_Funded_Windows_8_XAML_App"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Class="My_Ad_Funded_Windows_8_XAML_App.MainPage"
        IsTabStop="false"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        </Grid>
    </Page>
    

    Include the line of code xmlns:UI="using:Microsoft.Advertising.WinRT.UI" before x:Class="My_Ad_Funded_Windows_8_XAML_App.MainPage".

    The Page code should now 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_Ad_Funded_Windows_8_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_Ad_Funded_Windows_8_XAML_App.MainPage"
        IsTabStop="false"
        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="10043105" 
                HorizontalAlignment="Left" 
                Height="250" 
                Margin="0,0,0,0" 
                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_Ad_Funded_Windows_8_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_Ad_Funded_Windows_8_XAML_App.MainPage"
        IsTabStop="false"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <UI:AdControl 
                ApplicationId="d25517cb-12d4-4699-8bdc-52040c712cab" 
                AdUnitId="10043105" 
                HorizontalAlignment="Left" 
                Height="250" 
                Margin="0,0,0,0" 
                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

Consult the TestMode section for various AdUnitIds to see a variety of ads. Change the Height and Width properties in the XAML code (above) to match the ad size you choose.

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 8 Dev Center support page (scroll down to the section "Microsoft Advertising support").

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