3

對於UWP,我需要在Grid中的列寬度不同的大小。此外,平板電腦和智能手機的價值應該不同。是否可以在XAML(Xamarin.Forms)中混合使用OnIdiom和OnPlatform?

下面的代碼崩潰該應用

<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> 

類型OnIdiom.Phone中的xmlns http://xamarin.com/schemas/2014/forms

該代碼是在ViewCell未找到。所以我不能使用額外的ResourceDictionaryOnSizeAllocated()在代碼隱藏文件中不可用。

是否可以同時使用OnIdiomOnPlatform?這在代碼工作

回答

9

OnIdiom,就像OnPlatform是你要申報對象。在你的情況下,你將OnIdiom.Phone屬性設置爲沒有這些屬性的對象。

XAML中應該更像:

<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> 
+0

我試圖在本地機器上運行應用程序。這裏200寬度不被尊重。它不應該落入「平板電腦」類別嗎? – testing

+0

'Device.OS'是'Windows','Device.Idiom'是'Desktop'。 XAML中是否有可用的定義? 'WinPhone'之前工作正常。 – testing

+1

支持OnIdiom中的'Desktop'幾天前登陸https://github.com/xamarin/Xamarin.Forms/pull/420。該修補程序將出現在即將到來的2.3.4-pre1中 –

0

一種選擇是使用

if(Device.OS == TargetPlatform.Windows && Device.Idiom == TargetIdiom.Phone) 
    this.innerGrid.ColumnDefinitions.First().Width = new GridLength(100); 

覆蓋現有的XAML定義。 innerGrid是Xaml中使用的Grid的名稱。

1

Xamarin.Forms的XAML示例:

<OnPlatform x:TypeArguments="View"> 
    <OnPlatform.Android> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition> 
        <ColumnDefinition.Width> 
         <OnIdiom x:TypeArguments="GridLength" Tablet="100" Phone="50" /> 
        </ColumnDefinition.Width> 
       </ColumnDefinition> 
       <ColumnDefinition> 
        <ColumnDefinition.Width> 
         <OnIdiom x:TypeArguments="GridLength" Tablet="100" Phone="50" /> 
        </ColumnDefinition.Width> 
       </ColumnDefinition> 
      </Grid.ColumnDefinitions> 
     </Grid> 
    </OnPlatform.Android> 
    <OnPlatform.iOS> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition> 
        <ColumnDefinition.Width> 
         <OnIdiom x:TypeArguments="GridLength" Tablet="100" Phone="50" /> 
        </ColumnDefinition.Width> 
       </ColumnDefinition> 
       <ColumnDefinition> 
        <ColumnDefinition.Width> 
         <OnIdiom x:TypeArguments="GridLength" Tablet="100" Phone="50" /> 
        </ColumnDefinition.Width> 
       </ColumnDefinition> 
      </Grid.ColumnDefinitions> 
     </Grid> 
    </OnPlatform.iOS> 
</OnPlatform> 
+0

OK,我乘完整'Grid'定義。但是我有幾個名爲'Label'的標籤,並且不能有多個同名的標籤。任何解決方案? – testing

相關問題