Skip to content

Latest commit

 

History

History
77 lines (66 loc) · 3.77 KB

error-handling-in-xamlc-walkthrough.md

File metadata and controls

77 lines (66 loc) · 3.77 KB
author ms.assetid description title ms.author ms.date ms.topic ms.prod ms.technology keywords ms.localizationpriority
mcleanbyron
cf0d2709-21a1-4d56-9341-d4897e405f5d
Learn how to catch AdControl errors in your app.
Error handling in XAML/C# walkthrough
mcleans
08/23/2017
article
windows
uwp
windows 10, uwp, ads, advertising, error handling, XAML, c#
medium

Error handling in XAML/C# walkthrough

This walkthrough demonstrates how to catch ad-related errors in your app. This walkthrough uses an AdControl, but the general concepts in it also apply to InterstitialAd and NativeAdsManager.

These examples assume that you have a XAML/C# app that contains an AdControl. For step-by-step instructions that demonstrate how to add an AdControl to your app, see AdControl in XAML and .NET.

  1. In your MainPage.xaml file, locate the definition for the AdControl. That code looks like this.

    <UI:AdControl
      ApplicationId="3f83fe91-d6be-434d-a0ae-7351c5a997f1"
      AdUnitId="test"
      HorizontalAlignment="Left"
      Height="250"
      Margin="10,10,0,0"
      VerticalAlignment="Top"
      Width="300" />
  2. After the Width property, but before the closing tag, assign a name of an error event handler to the ErrorOccurred event. In this walkthrough, the name of the error event handler is OnAdError. xml <UI:AdControl ApplicationId="3f83fe91-d6be-434d-a0ae-7351c5a997f1" AdUnitId="test" HorizontalAlignment="Left" Height="250" Margin="10,10,0,0" VerticalAlignment="Top" Width="300" ErrorOccurred="OnAdError"/>

  3. To generate an error at runtime, create a second AdControl with a different application ID. Because all AdControl objects in an app must use the same application ID, creating an additional AdControl with a different application id will throw an error.

    Define a second AdControl in MainPage.xaml just after the first AdControl, and set the ApplicationId property to zero (“0”).

    <UI:AdControl
        ApplicationId="0"
        AdUnitId="test"
        HorizontalAlignment="Left"
        Height="250"
        Margin="10,265,0,0"
        VerticalAlignment="Top"
        Width="300"
        ErrorOccurred="OnAdError" />
  4. In MainPage.xaml.cs, add the following OnAdError event handler to the MainPage class. This event handler writes information to the Visual Studio Output window.

    private void OnAdError(object sender, AdErrorEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("AdControl error (" + ((AdControl)sender).Name +
            "): " + e.ErrorMessage + " ErrorCode: " + e.ErrorCode.ToString());
    }
  5. Build and run the project. After the app is running, you will see a message similar to the one below in the Output window of Visual Studio.

    AdControl error (): MicrosoftAdvertising.Shared.AdException: all ad requests must use the same application ID within a single application (0, d25517cb-12d4-4699-8bdc-52040c712cab) ErrorCode: ClientConfiguration
    

Related topics