是否可以在 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>