job.answiz.com
  • 0
Votes
name

I'm trying to insert a UWP specific child in the custom renderer of a StackLayout. However, in the sample code below, Control is always null whereas my StackLayout has Children. Maybe StackPanel is not what StackLayout is rendered into in UWP.

public class MyRenderer : ViewRenderer
{
    private bool _childAdded;

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (!_childAdded && Control?.Children != null)
        {
            _childAdded = true;
            Control.Children.Insert(0, new Windows.UI.Xaml.Shapes.Rectangle());
        }
        base.OnElementPropertyChanged(sender, e);
    }
}

Some modification in you are cade because you are calling base.OnElementPropertyChanged(sender,e) after code implementation. Just try to use below code.

public class MyRenderer : ViewRenderer
{
    private bool _childAdded;

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
         base.OnElementPropertyChanged(sender, e);
         if(Control==null)
          return;
        if (!_childAdded && Control.Children != null)
        {
            _childAdded = true;
            Control.Children.Insert(0, new Windows.UI.Xaml.Shapes.Rectangle());
        }

    }
}
  • 1
Reply Report