I have a simple Xamarin Forms content page defined as below
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyCustomView"
x:Class="MyCustomView.MyContentPage">
<ContentPage.Content>
<RelativeLayout>
<local:CustomView
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Constant=0, Factor=0}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Constant=0, Factor=0}"/>
</RelativeLayout>
</ContentPage.Content>
</ContentPage>
The local:CustomView
is defined as
public class CustomView : ContentView
{
public CustomView()
{
}
}
For which I have created the following custom renderer
[assembly: Xamarin.Forms.ExportRenderer(typeof(MyCustomView.CustomView), typeof(MyCustomView.Droid.CustomViewRendererDroid))]
namespace MyCustomView.Droid
{
public class CustomViewRendererDroid : ViewRenderer<MyFloatingActionButton.FloatingActionButton, View>
{
View _backgroundView;
protected override void OnElementChanged(ElementChangedEventArgs<FloatingActionButton> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && Control == null)
{
_backgroundView = new View(this.Context);
_backgroundView.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
_backgroundView.SetBackgroundColor(Color.Blue);
_backgroundView.BringToFront();
_backgroundView.SetMinimumHeight(100);
_backgroundView.SetMinimumWidth(100);
_backgroundView.Left = 50;
_backgroundView.Top = 100;
SetWillNotDraw(false);
SetNativeControl(_backgroundView);
}
}
}
}
No matter what I try (hence lots of positioning, coloring, etc in the renderer) I can't get this custom view to appear in a RelativeLayout, but if I change the XAML to use a StackLayout it appears no problem. What am I missing?