I want to create a custom control that is derived for example from a Grid and is defined in a XAML file (MyControl.xaml) and in a code-behind file (MyControl.xaml.cs). I want to be able to specify different implementations for different plaftorms, so that child controls are arranged for example horizontally on Android and vertically on iOS. I wanted to divide the XAML code into blocks per platform, so I tried using <OnPlatform xmlns="..." xmlns:x="..." x:Class="..." x:TypeArguments="View">
as the top level XAML node:
<OnPlatform xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyControls.MyControl"
x:TypeArguments="View">
<OnPlatform.Android>
<Grid>
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" x:Name="MyLabel1" />
...
</Grid>
</OnPlatform.Android>
<OnPlatform.iOS>
<Grid>
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
...
</Grid>
</OnPlatform.iOS>
</OnPlatform>
This made me define the class of my custom control as “public class MyControl : OnPlatform<View>
”. The compilation failed, because commands like “MyLabel1 = this.FindByName<Label>("MyLabel1");
” were generated in the “MyControl.xaml.g.cs” file in the “InitializeComponent” method and the “FindByName” method was reported as unrecognized.
How can I satisfy the condition that the structure of the custom control must be defined in XAML specifically for each platform?