Tuesday, February 20, 2018

Xamarin.Forms: How to properly use <OnPlatform> to pick which control to use in my TableSection?

Leave a Comment

I have a TableView with few sections. In my TableSection I want to display different controls depending on the platform. I want to use the usual TextCell for iOS and a custom ViewCell for Android. I have 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 above code does not work. It gives me an error: Object reference not set to an instance of an object.

Am I missing something? Should I be using a different x:TypeArguments?

Edit:

Edited to incorporate @Fahadsk answer and now getting the error: System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array. Anyone knows why I'm getting this?

1 Answers

Answers 1

OnPlatform is obsolete as of version 2.3.4.

Use following syntax in your code

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

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

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment