2016-07-25 48 views
0

嗨我在我的UWP應用程序中使用微軟廣告,我希望廣告調整後的應用程序,但無法得到它的工作。 我明白廣告控制必須在有效的尺寸(如描述here),所以我寫了這個代碼,以調整廣告:調整微軟廣告大小

private void panel_SizeChanged(object sender, SizeChangedEventArgs e) 
{ 
    if (e.NewSize.Width >= 728) 
    { 
     ad.Width = 728; 
     ad.Height = 90; 
    } 
    else if (e.NewSize.Width >= 640) 
    { 
     ad.Width = 640; 
     ad.Height = 100; 
    } 
    else if (e.NewSize.Width >= 480) 
    { 
     ad.Width = 480; 
     ad.Height = 80; 
    } 
    else if (e.NewSize.Width >= 320) 
    { 
     ad.Width = 320; 
     ad.Height = 50; 
    } 
    else if (e.NewSize.Width >= 300) 
    { 
     ad.Width = 300; 
     ad.Height = 50; 
    } 
} 

這使得控制相應的調整,但在控制內的廣告看起來可怕。我加了ad.Refresh();在最後,但這並沒有改變一件事。

有誰知道該怎麼辦?

回答

1

我遇到了同樣的問題。 不幸的是,廣告每隔30秒加載一次,你無法在30秒內再刷新一次。 這是因爲對Refresh()方法的調用失敗。 我使用了一種解決方法,希望它能幫助您。 我用廣告的相同大小(和位置)的StackPanel「覆蓋」廣告。我必須更改廣告的尺寸時才顯示此面板。 在廣告刷新時(您可以通過回調AdRefreshed攔截它),我已隱藏了封面。

<StackPanel x:Name="AdsCover" Width="300" Height="50" Visibility="Visible" 
        Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Left" VerticalAlignment="Bottom" Canvas.ZIndex="12" Background="WhiteSmoke"> 
      <Border x:Name="AdsBorder" BorderBrush="{x:Null}" Height="50"> 
       <TextBlock x:Name="AdsLoading" Text="Ads Loading..." HorizontalAlignment="Center" FontStyle="Italic" FontFamily="Calibri" FontSize="24" 
         TextAlignment="Center" VerticalAlignment="Center"/> 
      </Border> 
     </StackPanel> 
     <UI:AdControl x:Name="adsMS" 
       ApplicationId="3f83fe91-d6be-434d-a0ae-7351c5a997f1" 
       AdUnitId="10865270" 
       HorizontalAlignment="Left" 
       Height="50" 
       VerticalAlignment="Bottom" 
       Width="300" 
       Grid.Column="0" Grid.ColumnSpan="3" 
       Canvas.ZIndex="10" 
       ErrorOccurred="OnAdErrorOccurred" 
       AdRefreshed="OnAdRefreshed"/> 

在後面的代碼,在那裏你必須改變廣告的大小,你可以這樣做:

... 
    // Change the size of the Ad. adsW and adsH are the new size 
    adsMS->SetValue(WidthProperty, 1.0*adsW); 
    adsMS->SetValue(HeightProperty, 1.0*adsH); 
    // Cover panel with the same size 
    AdsCover->SetValue(WidthProperty, 1.0*adsW); 
    AdsCover->SetValue(HeightProperty, 1.0*adsH); 
    AdsBorder->SetValue(HeightProperty, 1.0*adsH); 

    // If the size are changed, I hide the Ad with the panel. 
    // In this way, I can avoid to see the deformed Ad. 
    // m_previousAdsWidth and m_previousAdsHeight are the previous size 
    // of the Ad. 
    if ((m_previousAdsWidth != adsW || m_previousAdsHeight != adsH) 
     && m_previousAdsWidth > 0 && m_previousAdsHeight > 0) 
    { 
     AdsCover->SetValue(VisibilityProperty, Windows::UI::Xaml::Visibility::Visible); 
    } 
    m_previousAdsWidth = adsW; 
    m_previousAdsHeight = adsH; 
    ... 

在OnAdRefreshed回調(),您可以隱藏面板

// Called when the Ad is refreshed. 
void DirectXPage::OnAdRefreshed(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) 
{ 
    // If the Ad is hidden by the cover panel, I will make it visible again. 
    if (AdsCover->Visibility == Windows::UI::Xaml::Visibility::Visible) 
     AdsCover->SetValue(VisibilityProperty, Windows::UI::Xaml::Visibility::Collapsed); 
} 
+0

這有點幫助我,謝謝! –