C# 中的间隙广告示例代码

警告

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

本主题提供了显示间隙视频广告的基本 C# 和 XAML 通用 Windows 平台 (UWP) 应用的完整示例代码。 有关显示如何配置项目以使用此代码的分步说明,请参阅间隙广告。 有关完整示例项目,请参阅 GitHub 上的广告示例

代码示例

本部分显示了显示间隙广告的基本应用中的 MainPage.xaml 和 MainPage.xaml.cs 文件内容。 若要使用这些示例,请将代码复制到 Visual Studio 中的 Visual C# 空白应用(通用 Windows)项目中。

此示例应用使用两个按钮请求和启动间隙广告。 将应用提交到应用商店之前,将 myAppIdmyAdUnitId 字段的值替换为合作伙伴中心的实时值。 有关详细信息,请参阅在应用中设置广告单元

注意

若要更改此示例以显示间隙横幅广告而不是间隙视频广告,请将值 AdType.Display 传递至 RequestAd 方法的第一个参数而不是 AdType.Video。 有关详细信息,请参阅间隙广告

MainPage.xaml

<Page
    x:Class="InterstitialAdSamplesCSharp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:InterstitialAdSamplesCSharp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Name="requestAdButton" Content="Request ad" Margin="37,244,0,364" Click="requestAdButton_Click"/>
        <Button Name="showAdButton" Content="Show ad" Margin="37,309,0,299" Click="showAdButton_Click"/>
    </Grid>
</Page>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Advertising.WinRT.UI;

namespace InterstitialAdSamplesCSharp
{
    public sealed partial class MainPage : Page
    {
        // Assign myAppId and myAdUnitId to test values. Replace these values with live values 
        // from Dev Center before you submit your app to the Store.
        InterstitialAd myInterstitialAd = null;
        string myAppId = "d25517cb-12d4-4699-8bdc-52040c712cab";
        string myAdUnitId = "test";

        public MainPage()
        {
            this.InitializeComponent();

            myInterstitialAd = new InterstitialAd();
            myInterstitialAd.AdReady += MyInterstitialAd_AdReady;
            myInterstitialAd.ErrorOccurred += MyInterstitialAd_ErrorOccurred;
            myInterstitialAd.Completed += MyInterstitialAd_Completed;
            myInterstitialAd.Cancelled += MyInterstitialAd_Cancelled;
        }

        // This method requests an interstitial ad when the "Request ad" button is clicked. In a real app, 
        // you should request the interstitial ad close to when you think it will be shown, but with 
        // enough advance time to make the request and prepare the ad (say 30 seconds to a few minutes).
        // To show an interstitial banner ad instead of an interstitial video ad, replace AdType.Video 
        // with AdType.Display.
        private void requestAdButton_Click(object sender, RoutedEventArgs e)
        {
            myInterstitialAd.RequestAd(AdType.Video, myAppId, myAdUnitId);
        }

        // This method attempts to show the interstitial ad when the "Show ad" button is clicked.
        private void showAdButton_Click(object sender, RoutedEventArgs e)
        {
            if (InterstitialAdState.Ready == myInterstitialAd.State)
            {
                myInterstitialAd.Show();
            }
        }

        void MyInterstitialAd_AdReady(object sender, object e)
        {
            // Your code goes here.
        }

        void MyInterstitialAd_ErrorOccurred(object sender, AdErrorEventArgs e)
        {
            // Your code goes here.
        }

        void MyInterstitialAd_Completed(object sender, object e)
        {
            // Your code goes here.
        }

        void MyInterstitialAd_Cancelled(object sender, object e)
        {
            // Your code goes here.
        }
    }
}