UWP + Forms 2.0 - Custom Page Renderer always shows empty

I have below custom page renderer for UWP app, while the app always shows an empty screen, from the debug with the Loaded event, I can see the border element in the ContainerElement with ActualWidth=0 and ActualHeight = 0, I think this is why it's empty. How can I get the element on the page rendered correctly?

protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            this.Loaded += JobsPageRenderer_Loaded;
            var container = this.ContainerElement as Windows.UI.Xaml.Controls.Panel;
            Windows.UI.Xaml.Controls.Border border = new Windows.UI.Xaml.Controls.Border { Height = 200, Width = 200, Background = new Windows.UI.Xaml.Media.SolidColorBrush(Colors.Red) };
            container.Children.Add(border);
        }

        private void JobsPageRenderer_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var container = this.ContainerElement as Windows.UI.Xaml.Controls.Panel;

            Windows.UI.Xaml.Controls.Border border = (Windows.UI.Xaml.Controls.Border)container.Children.First();

            border.UpdateLayout();//doesn't help

            container.UpdateLayout();//doesn't help

            //border's ActualHeight and ActualWidth are always 0
        }

Posts

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    Hi @jameszhou could you show your class declaration?
    Are you inheriting from the PageRenderer? Could you try inheriting from the ContentPageRenderer?

  • jameszhoujameszhou CNMember ✭✭

    Hi @GeraldVersluis It's PageRenderer, I didn't find ContentPageRenderer. Where is this class?

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭

    @jameszhou said:
    Hi @GeraldVersluis It's PageRenderer, I didn't find ContentPageRenderer. Where is this class?

    My mistake, there isn't one, sorry. Must be the end of the year ;)

  • jameszhoujameszhou CNMember ✭✭

    anyone can give light on this? custom page renderer for UWP works with Forms 2.0?

  • jameszhoujameszhou CNMember ✭✭

    I didn't figure out how to get the custom page renderer working with UWP, but I used a custom view renderer instead for my project and it works well.

  • RajanJohnRajanJohn USMember

    Use the SetNativeControl method instead of Container.Children.Add

    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
    base.OnElementChanged(e);
    if (Control == null)
    {
    var p = new WinUWPPage();
    this.SetNativeControl(p);
    }
    }
    This approach seems to be working in Windows 10 UWP.

  • Jason Smith answered the same question in Xamarin's Bugzilla:
    https://bugzilla.xamarin.com/show_bug.cgi?id=37432

    You need to implement ArrangeOverride and actually arrange the controls because the base renderer doesn't know what to do with these controls.

  • JohnHardmanJohnHardman GBUniversity mod

    @RajanJohn - What is WinUWPPage in that code snippet?

Sign In or Register to comment.