Xamarin表单中的PopAsync之后,UWP AdControl空白为空 [英] UWP AdControl blank after PopAsync in Xamarin Forms

查看:60
本文介绍了Xamarin表单中的PopAsync之后,UWP AdControl空白为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Microsoft Advertising AdControl的Xamarin Forms项目.打开页面时,该控件工作正常,但是在页面顶部的PopAsync之后再次显示该页面时,AdControl为空白,此后保持空白.

Xamarin Forms project using the Microsoft Advertising AdControl. The control works fine when a page is opened, but when a page is shown again after a PopAsync of a page on top of it, the AdControl is blank, and stays blank after that.

我在此处,但我无法使用答案和评论.

I found an old question seemingly on the same subject here, but I can't make any use of the answers and comments.

当带有AdControl的页面重新出现在其顶部的页面的PopAsync之后时,e.NewElement为null,e.OldElement具有AdControlView(我在PCL中的自定义视图).

When the page with the AdControl re-appears after a PopAsync of the page on top of it, e.NewElement is null and e.OldElement has the AdControlView (my custom view in the 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吗?

Is anyone using the AdControl in a Xamarin Forms UWP production app?

推荐答案

通过我的测试,当第二页调用 PopAsync()时,第二页实例将从导航堆栈中删除,新的最顶层页面成为活动页面,但当前的最顶层页面似乎不会创建新实例,但会缓存旧实例.这应该符合预期,但是您的 AdControl 也不会刷新,并且

By testing on my side, when the second page invoking PopAsync(), the second page instance to be removed from the navigation stack, with the new topmost page becoming the active page, but it seems like the current topmost page will not create a new instance, but cached the old instance. This should be as expected, but your AdControl is not refresh as well, and the Refresh event of AdControl will not be triggered that it stay blank.

要解决此问题,您可以考虑强制自己刷新控件.您可能需要覆盖 OnAppearing <包含 AdControl 的第一页中的/a>方法,因为返回的页面调用了此方法.返回第一页后,将调用 OnAppearing ,您可以根据需要在此方法中执行某些操作来强制刷新 AdControl .仅举例来说,在这里我重新初始化将起作用的页面:

To resolve this,you could consider to force refresh the control by yourself. You may need to override the OnAppearing method in the first page that contains the AdControl, since the page being returned to has this method override invoked. Once the first page returned, OnAppearing will be invoked, you could do something to force refresh the AdControl as you want inside this method. Just for example, here I re-initialize the page that will work:

public MainPage()
{
    InitializeComponent() 
}

async void OnButtonClicked(object sender, EventArgs args)
{
    await Navigation.PushAsync(new Page2());  
}
protected override void OnAppearing()
{
    base.OnAppearing();
    InitializeComponent();
}

更多详细信息,请参考此文档.

More details please reference this document.

这篇关于Xamarin表单中的PopAsync之后,UWP AdControl空白为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆