Xamarin Forms 中的 PopAsync 后 UWP AdControl 空白
UWP AdControl blank after PopAsync in Xamarin Forms
使用 Microsoft Advertising AdControl 的 Xamarin Forms 项目。打开页面时该控件工作正常,但是当页面在页面顶部的 PopAsync 之后再次显示时,AdControl 为空白,之后保持空白。
我发现了一个看似关于同一主题的旧问题 ,但我无法使用答案和评论。
当带有 AdControl 的页面在页面顶部的 PopAsync 之后重新出现时,e.NewElement 为空并且 e.OldElement 具有 AdControlView(我在 PCL).
UWP 渲染器:
public class AdViewRenderer : ViewRenderer<AdControlView, UWPAdView>
{
protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)
{
base.OnElementChanged(e);
if (null == Control && e.NewElement != null)
{
UWPAdView ad = new UWPAdView();
SetNativeControl(ad);
}
}
}
UWP项目中的AdControl用户控件:
<UserControl
x:Class="Sample.UWP.Helpers.UWPAdView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:aduwp="using:Microsoft.Advertising.WinRT.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d">
<Grid>
<aduwp:AdControl
Width="640"
Height="100"
HorizontalAlignment="Stretch"
AdUnitId="myadunitid"
ApplicationId="myappid"
AutoRefreshIntervalInSeconds="30"
ErrorOccurred="AdControl_ErrorOccurred"
IsAutoRefreshEnabled="True" />
</Grid>
</UserControl>
有人在 Xamarin Forms UWP 生产应用中使用 AdControl 吗?
通过我这边的测试,当第二个页面调用 PopAsync()
时,第二个页面实例将从导航堆栈中删除,新的最顶层页面成为活动页面,但看起来像当前最顶层页面不会创建新实例,而是缓存旧实例。这应该是预期的,但是你的 AdControl
也没有刷新,AdControl
的 Refresh
事件不会被触发,它保持空白。
要解决这个问题,你可以考虑自己强制刷新控件。您可能需要在包含 AdControl
的第一个页面中覆盖 OnAppearing
方法,因为返回的页面调用了此方法覆盖。返回第一页后,将调用 OnAppearing
,您可以在此方法中根据需要执行某些操作以强制刷新 AdControl
。举个例子,这里我重新初始化了可以正常工作的页面:
public MainPage()
{
InitializeComponent()
}
async void OnButtonClicked(object sender, EventArgs args)
{
await Navigation.PushAsync(new Page2());
}
protected override void OnAppearing()
{
base.OnAppearing();
InitializeComponent();
}
更多详情请参考this document。
使用 Microsoft Advertising AdControl 的 Xamarin Forms 项目。打开页面时该控件工作正常,但是当页面在页面顶部的 PopAsync 之后再次显示时,AdControl 为空白,之后保持空白。
我发现了一个看似关于同一主题的旧问题
当带有 AdControl 的页面在页面顶部的 PopAsync 之后重新出现时,e.NewElement 为空并且 e.OldElement 具有 AdControlView(我在 PCL).
UWP 渲染器:
public class AdViewRenderer : ViewRenderer<AdControlView, UWPAdView>
{
protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)
{
base.OnElementChanged(e);
if (null == Control && e.NewElement != null)
{
UWPAdView ad = new UWPAdView();
SetNativeControl(ad);
}
}
}
UWP项目中的AdControl用户控件:
<UserControl
x:Class="Sample.UWP.Helpers.UWPAdView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:aduwp="using:Microsoft.Advertising.WinRT.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d">
<Grid>
<aduwp:AdControl
Width="640"
Height="100"
HorizontalAlignment="Stretch"
AdUnitId="myadunitid"
ApplicationId="myappid"
AutoRefreshIntervalInSeconds="30"
ErrorOccurred="AdControl_ErrorOccurred"
IsAutoRefreshEnabled="True" />
</Grid>
</UserControl>
有人在 Xamarin Forms UWP 生产应用中使用 AdControl 吗?
通过我这边的测试,当第二个页面调用 PopAsync()
时,第二个页面实例将从导航堆栈中删除,新的最顶层页面成为活动页面,但看起来像当前最顶层页面不会创建新实例,而是缓存旧实例。这应该是预期的,但是你的 AdControl
也没有刷新,AdControl
的 Refresh
事件不会被触发,它保持空白。
要解决这个问题,你可以考虑自己强制刷新控件。您可能需要在包含 AdControl
的第一个页面中覆盖 OnAppearing
方法,因为返回的页面调用了此方法覆盖。返回第一页后,将调用 OnAppearing
,您可以在此方法中根据需要执行某些操作以强制刷新 AdControl
。举个例子,这里我重新初始化了可以正常工作的页面:
public MainPage()
{
InitializeComponent()
}
async void OnButtonClicked(object sender, EventArgs args)
{
await Navigation.PushAsync(new Page2());
}
protected override void OnAppearing()
{
base.OnAppearing();
InitializeComponent();
}
更多详情请参考this document。