Xamarin.Forms: How to use <OnPlatform> correctly to choose which control to use in my table?

I have a TableView with several sections. In my TableSection I want to display various controls depending on the platform. I want to use regular TextCell for iOS and custom ViewCell for Android. I tried the following code:

 <TableSection Title="Activity"> <OnPlatform x:TypeArguments="View"> <On Platform="iOS"> <TextCell x:Name="btnCountIOS" Text="Count" Height="50" /> <TextCell x:Name="cardHistory" Text="History" StyleId="disclosure" Tapped="openHistoryPage" Height="50"/> </On> <On Platform="Android"> <local:MyViewCell NoTap="true" Height="50"> <Grid VerticalOptions="CenterAndExpand" Padding="20, 0"> <Label Style="{DynamicResource ListItemTextStyle}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Today" /> <Label Style="{DynamicResource ListItemTextStyle}" TextColor="Gray" HorizontalOptions="End" x:Name="btnCountDroid" /> </Grid> </local:MyViewCell> <local:MyViewCell> <!-- more code here --> </local:MyViewCell> </On> </OnPlatform> </TableSection> 

The code above does not work. This gives me an error: Object reference not set to an instance of an object .

Am I missing something? Should I use another x:TypeArguments ?

Edit:

Edited to include @Fahadsk's answer and now get the error: System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array. Does anyone know why I am getting this?

+5
source share
1 answer

OnPlatform is deprecated since version 2.3.4.

Use the following syntax in code

 <OnPlatform x:TypeArguments="Thickness"> <On Platform="Android" Value="0, 0, 0, 0"/> <On Platform="iOS" Value="0, 20, 0, 0"/> </OnPlatform> 

For more information on this and pull request , also check out this blog.

+1
source

Source: https://habr.com/ru/post/1275302/


All Articles