Xamarin Forms - UWP custom renderer: how to add a child to a stacklayout
Xamarin Forms - UWP custom renderer: how to add a child to a stacklayout
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.
StackLayout
Control
null
StackLayout
StackPanel
StackLayout
public class MyRenderer : ViewRenderer<StackLayout, StackPanel>
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);
2 Answers
2
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<StackLayout, StackPanel>
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());
Children
null
Control
StackLayout
StackPanel
The StackLayout
(Layout
) renderer is ViewRenderer
and implemented on UWP by FrameworkElement
; Renderer Base Classes and Native Controls.
StackLayout
Layout
ViewRenderer
FrameworkElement
Theoretical renderer:
public class MyRenderer : ViewRenderer<StackLayout, FrameworkElement>
...
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Tks, but I don't see how starting with the base implementation would change anything. However, you made me realise that this is not
Children
which isnull
butControl
. Hence the issue is surely that theStackLayout
implementation in UWP may not beStackPanel
.– François
9 hours ago