调整 Microsoft Ads 的大小
Resizing Microsoft Ads
您好,我在我的 UWP 应用程序中使用 Microsofts Ads,我希望广告在应用程序结束后调整大小,但无法正常工作。
我了解广告控件必须采用有效尺寸(如 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();最后,但这并没有改变任何事情。
有人知道该怎么办吗?
我遇到了你同样的问题。
不幸的是,广告每 30 秒加载一次,您不能每 30 秒刷新一次。
这是因为调用 Refresh() 方法失败的原因。
我使用了一种解决方法,希望它能对您有所帮助。
我有 "covered" 具有与广告相同大小(和位置)的 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);
}
您好,我在我的 UWP 应用程序中使用 Microsofts Ads,我希望广告在应用程序结束后调整大小,但无法正常工作。 我了解广告控件必须采用有效尺寸(如 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();最后,但这并没有改变任何事情。
有人知道该怎么办吗?
我遇到了你同样的问题。 不幸的是,广告每 30 秒加载一次,您不能每 30 秒刷新一次。 这是因为调用 Refresh() 方法失败的原因。 我使用了一种解决方法,希望它能对您有所帮助。 我有 "covered" 具有与广告相同大小(和位置)的 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);
}