Interstitial ad sample code in C#


As of June 1, 2020, the Microsoft Ad Monetization platform for Windows UWP apps will be shut down. Learn more

This topic provides the complete sample code for a basic C# and XAML Universal Windows Platform (UWP) app that shows an interstitial video ad. For step-by-step instructions that show how to configure your project to use this code, see Interstitial ads. For a complete sample project, see the advertising samples on GitHub.

Code example

This section shows the contents of the MainPage.xaml and MainPage.xaml.cs files in a basic app that shows an interstitial ad. To use these examples, copy the code into a Visual C# Blank App (Universal Windows) project in Visual Studio.

This sample app uses two buttons to request and then launch an interstitial ad. Replace the values of the myAppId and myAdUnitId fields with live values from Partner Center before submitting your app to the Store. For more information, see Set up ad units in your app.


To alter this example to show an interstitial banner ad instead of an interstitial video ad, pass the value AdType.Display to the first parameter of the RequestAd method instead of AdType.Video. For more information, see Interstitial ads.


    <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"/>


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()

            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)

        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.