Xamarin.Forms:如何正确使用< OnPlatform>选择在TableSection中使用哪个控件? [英] Xamarin.Forms: How to properly use <OnPlatform> to pick which control to use in my TableSection?

查看:122
本文介绍了Xamarin.Forms:如何正确使用< OnPlatform>选择在TableSection中使用哪个控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableView,其中有几个部分.在我的TableSection中,我希望根据平台显示不同的控件.我想对iOS使用常规的TextCell,对于Android使用自定义ViewCell.我尝试了以下代码:

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>

上面的代码不起作用.它给我一个错误:Object reference not set to an instance of an object.

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

我错过了什么吗?我应该使用其他x:TypeArguments吗?

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

经过编辑以包含@Fahadsk答案,现在出现错误:System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.任何人都知道我为什么得到这个吗?

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?

推荐答案

从2.3.4版开始,OnPlatform已过时.

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>

有关和此拉请求,也请查看此

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

这篇关于Xamarin.Forms:如何正确使用&lt; OnPlatform&gt;选择在TableSection中使用哪个控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆