我有几节的TableView
。在我的TableSection
中,我想根据平台显示不同的控件。我想为iOS使用常规的TextCell
,为Android使用自定义的ViewCell
。我尝试了以下代码:
<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>
上面的代码不起作用。它给我一个错误:
Object reference not set to an instance of an object
。我想念什么吗?我应该使用其他
x:TypeArguments
吗?编辑:
编辑以合并@Fahadsk答案,现在得到错误:
System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
有人知道我为什么得到这个吗?
最佳答案
OnPlatform is obsolete as of version 2.3.4.
在代码中使用以下语法
<OnPlatform x:TypeArguments="Thickness">
<On Platform="Android" Value="0, 0, 0, 0"/>
<On Platform="iOS" Value="0, 20, 0, 0"/>
</OnPlatform>
有关this和此pull request的更多信息,也请查看此blog
关于xaml - Xamarin.Forms:如何正确使用<OnPlatform>来选择要在TableSection中使用的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48741131/