xaml - Xamarin.Forms:如何正确使用<OnPlatform>来选择要在TableSection中使用的控件?

标签 xaml xamarin xamarin.forms

我有几节的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/

相关文章:

c# - 屏幕宽度返回负数

c# - 当 WPF 中的文本更改时对 TexBox 强制执行验证

c# - 在连接的节点之间画线

.net - WPF 控件未显示在 WinForms 应用程序的 ElementHost 中

android - 使用 metadata.xml 删除绑定(bind) Xamarin 库中的 "virtual"关键字

xamarin - Prism :在 ViewModel 中设置值后 View 没有更新?

button - 多行文本按钮 Xamarin.Forms

wpf - 表格列宽和水平对齐方式

c# - 从 AppDelegate 访问类的现有实例中的方法

c# - Xamarin.Forms AdMob "Missing AdActivity"