Compartir a través de


Código de ejemplo de anuncios intersticiales en C#

Advertencia

A partir del 1 de junio de 2020, se cerrará la plataforma de monetización de anuncios de Microsoft para aplicaciones para UWP de Windows. Más información

En este tema se proporciona el código de ejemplo completo para una aplicación básica de C# y XAML Plataforma universal de Windows (UWP) que muestra un anuncio de vídeo intersticial. Para obtener instrucciones paso a paso que muestran cómo configurar el proyecto para usar este código, consulte Anuncios intersticiales. Para obtener un proyecto de ejemplo completo, consulte los ejemplos de publicidad en GitHub.

Ejemplo de código

En esta sección se muestra el contenido de los archivos MainPage.xaml y MainPage.xaml.cs en una aplicación básica que muestra un anuncio intersticial. Para usar estos ejemplos, copie el código en un proyecto aplicación en blanco de Visual C# (Windows universal) en Visual Studio.

Esta aplicación de ejemplo usa dos botones para solicitar y, a continuación, iniciar un anuncio intersticial. Reemplace los valores de los myAppId campos y myAdUnitId por valores dinámicos del Centro de partners antes de enviar la aplicación a la Tienda. Para obtener más información, consulta Configurar unidades de anuncios en la aplicación.

Nota:

Para modificar este ejemplo para mostrar un anuncio de banner intersticial en lugar de un anuncio de vídeo intersticial, pase el valor AdType.Display al primer parámetro del método RequestAd en lugar de AdType.Video. Para obtener más información, consulta Anuncios intersticiales.

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