是否可以在 XAML (Xamarin.Forms) 中混合使用 OnIdiom 和 OnPlatform? [英] Is it possible to mix OnIdiom and OnPlatform in XAML (Xamarin.Forms)?

查看:30
本文介绍了是否可以在 XAML (Xamarin.Forms) 中混合使用 OnIdiom 和 OnPlatform?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 UWP,Grid 中的列的宽度需要不同的大小.此外,平板电脑和智能手机上的值应该不同.

For UWP I need a different size for the width of a column in a Grid. Additionally, the value should be different on tablet and on smartphone.

以下代码使应用程序崩溃

The following code crashes the app

<ColumnDefinition>
    <ColumnDefinition.Width>
        <OnIdiom.Phone>
            <OnPlatform x:TypeArguments="GridLength" iOS="*" Android="*" WinPhone="100" />
        </OnIdiom.Phone>
        <OnIdiom.Tablet>
            <OnPlatform x:TypeArguments="GridLength" iOS="*" Android="*" WinPhone="200" />
        </OnIdiom.Tablet>
    </ColumnDefinition.Width>
</ColumnDefinition>

在 xmlns 中找不到类型 OnIdiom.Phone http://xamarin.com/schemas/2014/forms

Type OnIdiom.Phone not found in xmlns http://xamarin.com/schemas/2014/forms

代码在 ViewCell 中.所以我不能使用额外的 ResourceDictionary 并且 OnSizeAllocated() 在代码隐藏文件中不可用.

The code is in a ViewCell. So I can't use an additional ResourceDictionary and also OnSizeAllocated() is not available in the code behind file.

是否可以同时使用OnIdiomOnPlatform?

Is it possible to use OnIdiom and OnPlatform together?

推荐答案

OnIdiom,就像OnPlatform 是一个必须声明的对象.在您的情况下,您将 OnIdiom.Phone 属性设置为没有这些属性的对象.

OnIdiom, just like OnPlatform is an object you have to declare. In your case, you're setting OnIdiom.Phone property to an object that doesn't have those.

您的 Xaml 应该看起来更像:

Your Xaml should look more like:

<ColumnDefinition>
  <ColumnDefinition.Width>
    <OnIdiom x:TypeArguments="GridLength">
      <OnIdiom.Phone>
        <OnPlatform x:TypeArguments="GridLength" iOS="*" Android="*" WinPhone="100" />
      </OnIdiom.Phone>
      <OnIdiom.Tablet>
        <OnPlatform x:TypeArguments="GridLength" iOS="*" Android="*" WinPhone="200" />
      </OnIdiom.Tablet>
    </OnIdiom>
  </ColumnDefinition.Width>
</ColumnDefinition>

这篇关于是否可以在 XAML (Xamarin.Forms) 中混合使用 OnIdiom 和 OnPlatform?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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