XAML and C# troubleshooting guide
Warning
As of June 1, 2020, the Microsoft Ad Monetization platform for Windows UWP apps will be shut down. Learn more
This topic contains solutions to common development issues with the Microsoft advertising libraries in XAML apps.
XAML
AdControl not appearing
Ensure that the Internet (Client) capability is selected in Package.appxmanifest.
Check the application ID and ad unit ID. These IDs must match the application ID and ad unit ID that you obtained in Partner Center. For more information, see Set up ad units in your app.
<UI:AdControl AdUnitId="{AdUnitID}" ApplicationId="{ApplicationID}" Width="728" Height="90" />
Check the Height and Width properties. These must be set to one of the Supported ad sizes for banner ads.
<UI:AdControl AdUnitId="{AdUnitID}" ApplicationId="{ApplicationID}" Width="728" Height="90" />
Check the element position. The AdControl must be inside the viewable area.
Check the Visibility property. The optional Visibility property must not be set to collapsed or hidden. This property can be set inline (as shown below) or in an external style sheet.
<UI:AdControl AdUnitId="{AdUnitID}" ApplicationId="{ApplicationID}" Visibility="Visible" Width="728" Height="90" />
Check the parent of the AdControl. If the AdControl element resides in a parent element, the parent must be active and visible.
<StackPanel> <UI:AdControl AdUnitId="{AdUnitID}" ApplicationId="{ApplicationID}" Width="728" Height="90" /> </StackPanel>
Ensure the AdControl is not hidden from the viewport. The AdControl must be visible for ads to display properly.
Live values for ApplicationId and AdUnitId should not be tested in the emulator. To ensure the AdControl is functioning as expected, use the test values for both ApplicationId and AdUnitId.
Black box blinks and disappears
Double-check all steps in the previous AdControl not appearing section.
Handle the ErrorOccurred event, and use the message that is passed to the event handler to determine whether an error occurred and what type of error was thrown. See Error handling in XAML/C# walkthrough for more information.
This example demonstrates an ErrorOccurred event handler. The first snippet is the XAML UI markup.
<UI:AdControl AdUnitId="{AdUnitID}" ApplicationId="{ApplicationID}" Width="728" Height="90" ErrorOccurred="adControl_ErrorOccurred" /> <TextBlock x:Name="TextBlock1" TextWrapping="Wrap" Width="500" Height="250" />
This example demonstrates the corresponding C# code.
private void adControl_ErrorOccurred(object sender, Microsoft.Advertising.WinRT.UI.AdErrorEventArgs e) { TextBlock1.Text = e.ErrorMessage; }
The most common error that causes a black box is “No ad available.” This error means there is no ad available to return from the request.
The AdControl is behaving normally.
By default, the AdControl will collapse when it cannot display an ad. If other elements are children of the same parent they may move to fill the gap of the collapsed AdControl and expand when the next request is made.
Ads not refreshing
Check the IsAutoRefreshEnabled property. By default, this optional property is set to True. When set to False, the Refresh method must be used to retrieve another ad.
<UI:AdControl AdUnitId="{AdUnitID}" ApplicationId="{ApplicationID}" Width="728" Height="90" IsAutoRefreshEnabled="True" />
Check calls to the Refresh method. When using automatic refresh, Refresh cannot be used to retrieve another ad. When using manual refresh, Refresh should be called only after a minimum of 30 to 60 seconds depending on the device’s current data connection.
The following code snippets show an example of how to use the Refresh method. The first snippet is the XAML UI markup.
<UI:AdControl x:Name="adControl1" AdUnitId="{AdUnit_ID}" ApplicationId="{ApplicationID}" Width="728" Height="90" IsAutoRefreshEnabled="False" />
This code snippet shows an example of the C# code behind the UI markup.
public RefreshAds() { var timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(60) }; timer.Tick += (s, e) => adControl1.Refresh(); timer.Start(); }
The AdControl is behaving normally. Sometimes the same ad will appear more than once in a row giving the appearance that ads are not refreshing.
C#
AdControl not appearing
Ensure that the Internet (Client) capability is selected in Package.appxmanifest.
Ensure the AdControl is instantiated. If the AdControl is not instantiated it will not be available.
using Microsoft.Advertising.WinRT.UI; namespace AdControlExample { public sealed partial class MainPage : Page { AdControl myAdControl; public MainPage() { this.InitializeComponent(); myAdControl = new AdControl() { ApplicationId = "{ApplicationID}", AdUnitId = "{AdUnitID}", Height = 90, Width = 728 }; } } }
Check the application ID and ad unit ID. These IDs must match the application ID and ad unit ID that you obtained in Partner Center. For more information, see Set up ad units in your app.
adControl = new AdControl(); adControl.ApplicationId = "{ApplicationID}";adControl.AdUnitId = "{AdUnitID}"; adControl.Height = 90; adControl.Width = 728;
Check the Height and Width parameters. These must be set to one of the supported ad sizes for banner ads.
adControl = new AdControl(); adControl.ApplicationId = "{ApplicationID}"; adControl.AdUnitId = "{AdUnitID}"; adControl.Height = 90;adControl.Width = 728;
Ensure the AdControl is added to a parent element. To display, the AdControl must be added as a child to a parent control (for example, a StackPanel or Grid).
ContentPanel.Children.Add(adControl);
Check the Margin parameter. The AdControl must be inside the viewable area.
Check the Visibility property. The optional Visibility property must be set to Visible.
adControl = new AdControl(); adControl.ApplicationId = "{ApplicationID}"; adControl.AdUnitId = "{AdUnitID}"; adControl.Height = 90; adControl.Width = 728; adControl.Visibility = System.Windows.Visibility.Visible;
Check the parent of the AdControl. The parent must be active and visible.
Live values for ApplicationId and AdUnitId should not be tested in the emulator. To ensure the AdControl is functioning as expected, use the test values for both ApplicationId and AdUnitId.
Black box blinks and disappears
Double-check all steps in the AdControl not appearing section above.
Handle the ErrorOccurred event, and use the message that is passed to the event handler to determine whether an error occurred and what type of error was thrown. See Error handling in XAML/C# walkthrough for more information.
The following examples show the basic code needed to implement an error call. This XAML code defines a TextBlock that is used to display the error message.
<TextBlock x:Name="TextBlock1" TextWrapping="Wrap" Width="500" Height="250" />
This C# code retrieves the error message and displays it in the TextBlock.
using Microsoft.Advertising.WinRT.UI; namespace AdControlExample { public partial class MainPage : Page { AdControl myAdControl; public MainPage() { this.InitializeComponent(); myAdControl = new AdControl(); myAdControl.ApplicationId = "{ApplicationID}"; myAdControl.AdUnitId = "{AdUnitID}"; myAdControl.Height = 90; myAdControl.Width = 728; myAdControl.ErrorOccurred += (s,e) => { TextBlock1.Text = e.Error.Message; }; } } }
The most common error that causes a black box is “No ad available.” This error means there is no ad available to return from the request.
AdControl is behaving normally. Sometimes the same ad will appear more than once in a row giving the appearance that ads are not refreshing.
Ads not refreshing
Check whether the IsAutoRefreshEnabled property of your AdControl is set to false. By default, this optional property is set to true. When set to false, the Refresh method must be used to retrieve another ad.
Check calls to the Refresh method. When using automatic refresh (IsAutoRefreshEnabled is true), Refresh cannot be used to retrieve another ad. When using manual refresh (IsAutoRefreshEnabled is false), Refresh should be called only after a minimum of 30 to 60 seconds depending on the device’s current data connection.
The following example demonstrates how to call the Refresh method.
AdControl myAdControl; public MainPage() { InitializeComponent(); myAdControl = new AdControl(); myAdControl.ApplicationId = "{ApplicationID}"; myAdControl.AdUnitId = "{AdUnitID}"; myAdControl.Height = 90; myAdControl.Width = 728; myAdControl.IsAutoRefreshEnabled = false; ContentPanel.Children.Add(myAdControl); var timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(60) }; timer.Tick += (s, e) => myAdControl.Refresh(); timer.Start(); }
The AdControl is behaving normally. Sometimes the same ad will appear more than once in a row giving the appearance that ads are not refreshing.