XAML 和 C# 的疑難排解指南

警告

自 2020 年 6 月 1 日起,Windows UWP 應用程式的 Microsoft 廣告收益平台將會關閉。 深入了解

本主題包含 XAML 應用程式中 Microsoft Advertising 程式庫常見開發問題的解決方案。

XAML

AdControl 未出現

  1. 確定 Package.appxmanifest 中已選取 [網際網路 (用戶端] 功能。

  2. 檢查應用程式識別碼和廣告單元識別碼。 這些識別碼必須符合您在合作夥伴中心取得的應用程式識別碼和廣告單位識別碼。 如需詳細資訊,請參閱「在您的應用程式中設定廣告單元」。

    <UI:AdControl AdUnitId="{AdUnitID}" ApplicationId="{ApplicationID}"
                  Width="728" Height="90" />
    
  3. 檢查高度寬度 屬性。 這些必須設定為其中一種支援的橫幅廣告大小

    <UI:AdControl AdUnitId="{AdUnitID}"
                  ApplicationId="{ApplicationID}"
                  Width="728" Height="90" />
    
  4. 檢查元素定位。 AdControl 必須位於可檢視的區域內。

  5. 檢查可視性 屬性。 選用的可視性 屬性不得設定為折疊或隱藏。 這個屬性可以設定內嵌 (如下所示),或在外部樣式表中設定。

    <UI:AdControl AdUnitId="{AdUnitID}"
                  ApplicationId="{ApplicationID}"
                  Visibility="Visible"
                  Width="728" Height="90" />
    
  6. 檢查 AdControl 的父系。 如果 AdControl 元素位於父元素中,則父系必須處於作用中且可見。

    <StackPanel>
        <UI:AdControl AdUnitId="{AdUnitID}"
                      ApplicationId="{ApplicationID}"
                      Width="728" Height="90" />
    </StackPanel>
    
  7. 確定檢視區中沒有隱藏 AdControl AdControl 必須可見,廣告才能正確顯示。

  8. 模擬器中不應測試 ApplicationIdAdUnitId 的即時值。 若要確保 AdControl 如預期般運作,請使用 ApplicationIdAdUnitId測試值

  1. 請仔細檢查上一個 AdControl 未出現部分中的所有步驟。

  2. 處理 ErrorOccurred 事件,並使用傳遞至事件處理常式的訊息來判斷是否發生錯誤,以及擲回何種類型的錯誤。 如需詳細資訊,請參閱 XAML/C# 中的錯誤處理逐步解說

    此範例示範 ErrorOccurred 事件處理程式。 第一個片段是 XAML UI 標記。

    <UI:AdControl AdUnitId="{AdUnitID}"
                  ApplicationId="{ApplicationID}"
                  Width="728" Height="90"
                  ErrorOccurred="adControl_ErrorOccurred" />
    <TextBlock x:Name="TextBlock1" TextWrapping="Wrap" Width="500" Height="250" />
    

    此範例示範對應的 C# 程式碼。

    private void adControl_ErrorOccurred(object sender,               
        Microsoft.Advertising.WinRT.UI.AdErrorEventArgs e)
    {
        TextBlock1.Text = e.ErrorMessage;
    }
    

    造成黑箱最常見的錯誤是「沒有可用的廣告」。 此錯誤表示沒有可從要求傳回的廣告。

  3. AdControl 的行為正常。

    根據預設,AdControl 會在無法顯示廣告時摺疊。 如果其他元素是相同父系的子系,則它們可能會移動以填滿已摺疊的 AdControl 的空隙,並在下一個要求提出時展開。

廣告未重新整理

  1. 檢查isAutoRefreshEnabled 屬性。 根據預設,此選擇性屬性會設為 True。 當設定為 False 時,必須使用 Refresh 方法來擷取另一個廣告。

    <UI:AdControl AdUnitId="{AdUnitID}"
                  ApplicationId="{ApplicationID}"
                  Width="728" Height="90"
                  IsAutoRefreshEnabled="True" />
    
  2. 檢查 Refresh 方法的呼叫。 使用自動重新整理時,無法使用 Refresh 來擷取另一個廣告。 使用手動重新整理時,取決於裝置目前的資料連線,應該在至少 30 到 60 秒之後才呼叫 Refresh

    下面的程式碼片段展現如何使用 Refresh 方法的範例。 第一個片段是 XAML UI 標記。

    <UI:AdControl x:Name="adControl1"
                  AdUnitId="{AdUnit_ID}"
                  ApplicationId="{ApplicationID}"
                  Width="728" Height="90"
                  IsAutoRefreshEnabled="False" />
    

    此程式碼片段顯示 UI 標記後方的 C# 程式碼範例。

    public RefreshAds()
    {
        var timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(60) };
        timer.Tick += (s, e) => adControl1.Refresh();
        timer.Start();
    }
    
  3. AdControl 的行為正常。 有時候,相同的廣告會在一個資料列中出現多次,而有廣告未重新整理的假象。

C#

AdControl 未出現

  1. 確定 Package.appxmanifest 中已選取 [網際網路 (用戶端] 功能。

  2. 確定 AdControl 已具現化。 如果 AdControl未具現化,就無法使用。

    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
                };
            }
        }
    }
    
  3. 檢查應用程式識別碼和廣告單元識別碼。 這些識別碼必須符合您在合作夥伴中心取得的應用程式識別碼和廣告單位識別碼。 如需詳細資訊,請參閱「在您的應用程式中設定廣告單元」。

    adControl = new AdControl();
    adControl.ApplicationId = "{ApplicationID}";adControl.AdUnitId = "{AdUnitID}";
    adControl.Height = 90;
    adControl.Width = 728;
    
  4. 檢查高度寬度 參數。 這些必須設定為其中一種支援的橫幅廣告大小

    adControl = new AdControl();
    adControl.ApplicationId = "{ApplicationID}";
    adControl.AdUnitId = "{AdUnitID}";
    adControl.Height = 90;adControl.Width = 728;
    
  5. 確定 AdControl 已新增至父元素。 如欲顯示,AdControl 必須新增為父控制項的子控制項(例如 StackPanelGrid)。

    ContentPanel.Children.Add(adControl);
    
  6. 檢查 Margin 參數。 AdControl 必須位於可檢視的區域內。

  7. 檢查可視性 屬性。 選擇性可見性 屬性必須設定為可見

    adControl = new AdControl();
    adControl.ApplicationId = "{ApplicationID}";
    adControl.AdUnitId = "{AdUnitID}";
    adControl.Height = 90;
    adControl.Width = 728;
    adControl.Visibility = System.Windows.Visibility.Visible;
    
  8. 檢查 AdControl 的父系。 父系必須處於作用中且可見。

  9. 模擬器中不應測試 ApplicationIdAdUnitId 的即時值。 若要確保 AdControl 如預期般運作,請使用 ApplicationIdAdUnitId測試值

  1. 請仔細檢查前述 AdControl 未出現部分中的所有步驟。

  2. 處理 ErrorOccurred 事件,並使用傳遞至事件處理常式的訊息來判斷是否發生錯誤,以及擲回何種類型的錯誤。 如需詳細資訊,請參閱 XAML/C# 中的錯誤處理逐步解說

    下列範例展現實作錯誤呼叫所需的基本程式碼。 此 XAML 程式碼會定義用來顯示錯誤訊息的 TextBlock

    <TextBlock x:Name="TextBlock1" TextWrapping="Wrap" Width="500" Height="250" />
    

    此 C# 程式碼會擷取錯誤訊息,並顯示在 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;
                };
            }
        }
    }
    

    造成黑箱最常見的錯誤是「沒有可用的廣告」。 此錯誤表示沒有可從要求傳回的廣告。

  3. AdControl 的行為正常。 有時候,相同的廣告會在一個資料列中出現多次,而有廣告未重新整理的假象。

廣告未重新整理

  1. 檢查 AdControlIsAutoRefreshEnabled 屬性是否設定為 false。 根據預設,此選擇性屬性會設為 true。 當設定為 false 時,必須使用 Refresh 方法來擷取另一個廣告。

  2. 檢查 Refresh 方法的呼叫。 使用自動重新整理時 (IsAutoRefreshEnabledtrue),則無法使用 Refresh 來擷取另一個廣告。 使用手動重新整理時 (IsAutoRefreshEnabledfalse),視裝置目前的資料連線而定,應該在至少 30 到 60 秒之後才呼叫 Refresh

    下列範例將示範如何呼叫Refresh方法。

    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();
    }
    
  3. AdControl 的行為正常。 有時候,相同的廣告會在一個資料列中出現多次,而有廣告未重新整理的假象。