C# UWP 添加广告

WP

  1. 安装 Microsoft Advertising SDK for Windows Phone 8.1 (XAML)
  2. 添加引用 Microsoft Advertising SDK for Windows Phone 8.1 (XAML)
    xmlns:Myui="using:Microsoft.Advertising.Mobile.UI"

    <StackPanel>
        <Myui:AdControl ApplicationId="3f83fe91-d6be-434d-a0ae-7351c5a997f1"
          AdUnitId="10865270"
          HorizontalAlignment="Left"
          Height="80"
          VerticalAlignment="Top"
          Width="480"/>
    </StackPanel>

UWP

  1. 安装 Microsoft Advertising SDK for XAML
  2. 添加引用 Microsoft Advertising SDK for XAML
using Microsoft.Advertising.WinRT.UI;

public InterstitialAd myInterstitialAd = null;
public string myAppId = "d25517cb-12d4-4699-8bdc-52040c712cab";
public string myAdUnitId = "11389925";
myInterstitialAd = new InterstitialAd();
myInterstitialAd.AdReady += MyInterstitialAd_AdReady;
myInterstitialAd.ErrorOccurred += MyInterstitialAd_ErrorOccurred;
myInterstitialAd.Completed += MyInterstitialAd_Completed;
myInterstitialAd.Cancelled += MyInterstitialAd_Cancelled;
myInterstitialAd.RequestAd(AdType.Video, myAppId, myAdUnitId);

void MyInterstitialAd_AdReady(object sender, object e)
{
    // Your code goes here.
    Debug.WriteLine(String.Format("Ads ready"));
}

void MyInterstitialAd_ErrorOccurred(object sender, AdErrorEventArgs e)
{
    // Your code goes here.
    Debug.WriteLine(String.Format("Ads err, ask for next"));
    myInterstitialAd.RequestAd(AdType.Video, myAppId, myAdUnitId);
}

void MyInterstitialAd_Completed(object sender, object e)
{
    // Your code goes here.
    Debug.WriteLine(String.Format("Ads complete, ask for next"));
    myInterstitialAd.RequestAd(AdType.Video, myAppId, myAdUnitId);
}

void MyInterstitialAd_Cancelled(object sender, object e)
{
    // Your code goes here.
    Debug.WriteLine(String.Format("Ads canceled, ask for next"));
    myInterstitialAd.RequestAd(AdType.Video, myAppId, myAdUnitId);
}

//调用
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (InterstitialAdState.Ready == myInterstitialAd.State)
    {
        myInterstitialAd.Show();
    }
}

类似文章