Search code examples
formsxamarinuwpads

UWP AdControl blank after PopAsync in Xamarin Forms


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.

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).

The UWP renderer:

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);
        }
    }
}

The AdControl user control in the UWP project:

<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>

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


Solution

  • 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.

    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.