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

代码在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?

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>