原生广告

警告

截至 2020 年 6 月 1 日,Windows UWP 应用的Microsoft广告盈利平台将关闭。 了解详细信息

原生广告是一种基于组件的广告格式,其中广告创意的每个部分(例如标题、图像、描述和行动号召文本)作为单个元素传送到您的应用。 可以使用自己的字体、颜色、动画和其他 UI 组件将这些元素集成到应用中,以将不显眼的用户体验拼凑在一起,适合你的应用的外观,同时从广告中获得高收益。

对于广告商来说,原生广告提供高效的广告位,因为广告体验紧密集成到应用中,因此用户更倾向于与此类广告互动。

注释

当前仅支持基于 XAML 的适用于 Windows 10 和 Windows 11 的 UWP 应用的原生广告。 计划在 Microsoft 广告 SDK 的未来版本中支持使用 HTML 和 JavaScript 编写的 UWP 应用。

先决条件

将原生广告集成到应用中

按照以下说明将本机广告集成到应用中,并确认本机广告实现是否显示测试广告。

  1. 在 Visual Studio 中,打开项目或创建新项目。

    注释

    如果使用现有项目,请在项目中打开 Package.appxmanifest 文件,并确保已选择 Internet(客户端) 功能。 你的应用需要此功能才能接收测试广告和实时广告。

  2. 如果项目面向 任意 CPU,请更新项目以使用面向特定架构的构建输出(例如,x86)。 如果项目面向 任意 CPU,则在以下步骤中无法成功添加对 Microsoft 广告 SDK 的引用。 有关详细信息,请参阅在项目中将目标设为任意 CPU 导致的参考错误

  3. 在项目中添加对Microsoft广告 SDK 的引用:

    1. 解决方案资源管理器 窗口中,右键点击 引用,然后选择 添加引用...
    2. 引用管理器中,展开 Universal Windows,单击 扩展,然后选中 Microsoft Advertising SDK for XAML(版本 10.0)旁的复选框。
    3. 引用管理器中,单击“确定”。
  4. 在应用中的相应代码文件中(例如,在MainPage.xaml.cs或其他页面的代码文件中),添加以下命名空间引用。

    using Microsoft.Advertising.WinRT.UI;
    using Windows.UI.Xaml.Media.Imaging;
    
  5. 在应用的相应位置(例如,在 MainPage 或其他页面中),声明 NativeAdsManagerV2 对象以及表示本机广告的应用程序 ID 和广告单元 ID 的多个字符串字段。 下面的代码示例将 myAppIdmyAdUnitId 字段分配给原生广告测试值

    注释

    每个 NativeAdsManagerV2 都有一个对应的 广告单元,该单元被我们的服务用来向本机广告控件投放广告,每个广告单元都包含一个 广告单元 ID应用程序 ID。 在这些步骤中,你将测试广告单元 ID 和应用程序 ID 值分配给控件。 这些测试值只能在应用的测试版本中使用。 在将应用发布到应用商店之前,您必须 将这些测试值替换为来自合作伙伴中心的 实时值。

    NativeAdsManagerV2 myNativeAdsManager = null;
    string myAppId = "d25517cb-12d4-4699-8bdc-52040c712cab";
    string myAdUnitId = "test";
    
  6. 在启动时运行的代码中(例如,在页面的构造函数中),实例化 NativeAdsManagerV2 对象,并为该对象的 AdReadyErrorOccurred 事件设置事件处理程序。

    myNativeAdsManager = new NativeAdsManagerV2(myAppId, myAdUnitId);
    myNativeAdsManager.AdReady += MyNativeAd_AdReady;
    myNativeAdsManager.ErrorOccurred += MyNativeAdsManager_ErrorOccurred;
    
  7. 准备好显示本机广告时,请调用 RequestAd 方法来获取广告。

    myNativeAdsManager.RequestAd();
    
  8. 为应用准备本机广告时,将调用 AdReady 事件处理程序,并将表示本机广告的 NativeAdV2 对象传递给 e 参数。 使用 NativeAdV2 属性获取原生广告的每个元素,并在页面上显示这些元素。 请务必调用 RegisterAdContainer 方法来注册作为原生广告容器的 UI 元素,这是正确跟踪广告展示和点击所必需的。

    注释

    应用中的原生广告中有一些元素是必需的,并且必须始终显示。 有关详细信息,请参阅我们的 原生广告指南。

    例如,假设您的应用包含一个带有以下 MainPage 页面(或其他页面)。 此 StackPanel 包含一系列控件,这些控件显示本机广告的不同元素,包括由 文本赞助的标题、说明、图像、,以及显示 操作调用 文本的按钮。

    <StackPanel x:Name="NativeAdContainer" Background="#555555" Width="Auto" Height="Auto"
                Orientation="Vertical">
        <Image x:Name="AdIconImage" HorizontalAlignment="Left" VerticalAlignment="Center"
               Margin="20,20,20,20"/>
        <TextBlock x:Name="TitleTextBlock" HorizontalAlignment="Left" VerticalAlignment="Center"
               Text="The ad title will go here" FontSize="24" Foreground="White" Margin="20,0,0,10"/>
        <TextBlock x:Name="DescriptionTextBlock" HorizontalAlignment="Left" VerticalAlignment="Center"
                   Foreground="White" TextWrapping="Wrap" Text="The ad description will go here"
                   Margin="20,0,0,0" Visibility="Collapsed"/>
        <Image x:Name="MainImageImage" HorizontalAlignment="Left"
               VerticalAlignment="Center" Margin="20,20,20,20" Visibility="Collapsed"/>
        <Button x:Name="CallToActionButton" Background="Gray" Foreground="White"
                HorizontalAlignment="Left" VerticalAlignment="Center" Width="Auto" Height="Auto"
                Content="The call to action text will go here" Margin="20,20,20,20"
                Visibility="Collapsed"/>
        <StackPanel x:Name="SponsoredByStackPanel" Orientation="Horizontal" Margin="20,20,20,20">
            <TextBlock x:Name="SponsoredByTextBlock" Text="The ad sponsored by text will go here"
                       FontSize="24" Foreground="White" Margin="20,0,0,0" HorizontalAlignment="Left"
                       VerticalAlignment="Center" Visibility="Collapsed"/>
            <Image x:Name="IconImageImage" Margin="40,20,20,20" HorizontalAlignment="Left"
                   VerticalAlignment="Center" Visibility="Collapsed"/>
        </StackPanel>
    </StackPanel>
    

    下面的代码示例演示了一个 AdReady 事件处理程序,该程序负责在 StackPanel 控件中显示本机广告的每个元素,然后调用 RegisterAdContainer 方法,以注册 StackPanel。 此代码假定它从包含 StackPanel的页面的代码隐藏文件运行。

    void MyNativeAd_AdReady(object sender, NativeAdReadyEventArgs e)
    {
        NativeAdV2 nativeAd = e.NativeAd;
    
        // Show the ad icon.
        if (nativeAd.AdIcon != null)
        {
            AdIconImage.Source = nativeAd.AdIcon.Source;
    
            // Adjust the Image control to the height and width of the 
            // provided ad icon.
            AdIconImage.Height = nativeAd.AdIcon.Height;
            AdIconImage.Width = nativeAd.AdIcon.Width;
        }
    
        // Show the ad title.
        TitleTextBlock.Text = nativeAd.Title;
    
        // Show the ad description.
        if (!string.IsNullOrEmpty(nativeAd.Description))
        {
            DescriptionTextBlock.Text = nativeAd.Description;
            DescriptionTextBlock.Visibility = Visibility.Visible;
        }
    
        // Display the first main image for the ad. Note that the service
        // might provide multiple main images. 
        if (nativeAd.MainImages.Count > 0)
        {
            NativeImage mainImage = nativeAd.MainImages[0];
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.UriSource = new Uri(mainImage.Url);
            MainImageImage.Source = bitmapImage;
    
            // Adjust the Image control to the height and width of the 
            // main image.
            MainImageImage.Height = mainImage.Height;
            MainImageImage.Width = mainImage.Width;
            MainImageImage.Visibility = Visibility.Visible;
        }
    
        // Add the call to action string to the button.
        if (!string.IsNullOrEmpty(nativeAd.CallToActionText))
        {
            CallToActionButton.Content = nativeAd.CallToActionText;
            CallToActionButton.Visibility = Visibility.Visible;
        }
    
        // Show the ad sponsored by value.
        if (!string.IsNullOrEmpty(nativeAd.SponsoredBy))
        {
            SponsoredByTextBlock.Text = nativeAd.SponsoredBy;
            SponsoredByTextBlock.Visibility = Visibility.Visible;
        }
    
        // Show the icon image for the ad.
        if (nativeAd.IconImage != null)
        {
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.UriSource = new Uri(nativeAd.IconImage.Url);
            IconImageImage.Source = bitmapImage;
    
            // Adjust the Image control to the height and width of the 
            // icon image.
            IconImageImage.Height = nativeAd.IconImage.Height;
            IconImageImage.Width = nativeAd.IconImage.Width;
            IconImageImage.Visibility = Visibility.Visible;
        }
    
        // Register the container of the controls that display
        // the native ad elements for clicks/impressions.
        nativeAd.RegisterAdContainer(NativeAdContainer);
    }
    
  9. ErrorOccurred 事件定义事件处理程序,以处理与本机广告相关的错误。 以下示例在测试期间将错误信息写入 Visual Studio 输出 窗口。

    private void MyNativeAdsManager_ErrorOccurred(object sender, NativeAdErrorEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("NativeAd error " + e.ErrorMessage +
            " ErrorCode: " + e.ErrorCode.ToString());
    }
    
  10. 编译并运行应用,以通过测试广告查看它。

将您的应用与实时广告一起发布

确认本机广告实现成功显示测试广告后,请按照以下说明将应用配置为显示真实广告并将更新的应用提交到应用商店。

  1. 确保你的原生广告实现遵循我们的 原生广告指南

  2. 在合作伙伴中心,转到 应用内广告 页面,创建广告单元。 对于广告单元类型,请指定 Native。 记下广告单元 ID 和应用程序 ID。

    注释

    测试广告单元和实时 UWP 广告单元的应用程序 ID 值具有不同的格式。 测试应用程序 ID 值为 GUID。 在合作伙伴中心创建实时 UWP 广告单元时,广告单元的应用程序 ID 值始终与应用的应用商店 ID 匹配(例如应用商店 ID 值类似于 9NBLGGH4R315)。

  3. 可以选择在 应用内广告 页面的 中介设置 部分进行配置,来为原生广告启用广告中介。 通过广告中介,可以通过显示来自多个广告网络的广告来最大化广告收入和应用推广功能。

  4. 在代码中,将测试广告单元值(即 applicationIdadUnitIdNativeAdsManagerV2 构造函数的参数)替换为在合作伙伴中心生成的实时值。

  5. 使用合作伙伴中心将应用 提交到应用商店。

  6. 在合作伙伴中心查看您的 广告性能报告

在应用程序中管理多个原生广告的广告单元

可以在单个应用中使用多个原生广告位。 在此方案中,建议为每个原生广告位置分配不同的广告单元。 使用本机广告的不同广告单元可以单独 配置中介设置,并为每个控件获取离散 报告数据。 这也使我们的服务能够更好地优化我们为你的应用提供的广告。

重要

只能在一个应用中使用每个广告单元。 如果在多个应用中使用广告单元,则不会为该广告单元提供广告。