Xamarin Forms - UWP custom renderer: how to add a child to a stacklayout
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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