I want to have an Android.Widget.RelativeLayout added in a ContentPage. For this I've followed multiple online examples:
Xamarin Forms Custom Renderer for Android not displaying
Adding an "Android.Views.ViewGroup" to a Xamarin XAML page
and others.
But I cannot get it working.
In my shared project I have:
namespace SharedProject
{
public partial class App : Application
{
public App ()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
....
}
space
namespace SharedProject
{
public class MyCustomViewControl : View
{
public MyCustomViewControl()
{
}
}
}
and the MainPage.xaml which is being rendered:
<?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:SharedProject="clr-namespace:SharedProject;assembly=SharedProject"
x:Class="SharedProject.MainPage">
<ContentPage.Content>
<StackLayout>
<SharedProject:MyCustomViewControl
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
and in the MonoAndroid project:
[assembly: ExportRenderer(typeof(MyCustomViewControl), typeof(MyCustomViewRenderer))]
namespace SharedProject.Mobile.Droid
{
public class MyCustomViewRenderer : ViewRenderer<MyCustomViewControl, RelativeLayout>
{
public MyCustomViewRenderer(Context context) : base(context)
{
}
private Android.Widget.RelativeLayout rlMainContainer;
protected override void OnElementChanged(ElementChangedEventArgs<MyCustomViewControl> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
rlMainContainer = new RelativeLayout(Context);
rlMainContainer.SetMinimumHeight(50);
rlMainContainer.SetMinimumWidth(100);
rlMainContainer.LayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
if (Control == null)
SetNativeControl(rlMainContainer);
MainActivity.instance.setActiveReader(Control);
}
}
}
And in my MainActivity.OnCreate I have:
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
instance = this;
LoadApplication(new App());
I was expecting that when the MainPage is being rendered, the rlMainContainer would be also added to it as per the MainPage.xaml and OnElementChanged, but nothing inside MyCustomViewRenderer is being requested(no breakpoint hits).
What am I doing wrong?