您好我在我的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();最后,但这并没有改变一件事。
有人知道该怎么办吗?
答案 0 :(得分: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);
}