Is it possible to mix OnIdiom and OnPlatform in XAML (Xamarin.Forms)?

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

The following application failure code

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

with

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

The code is in ViewCell . Therefore, I cannot use an additional ResourceDictionary , and OnSizeAllocated() not available in a file that is outside the code.

Can I use OnIdiom and OnPlatform together?

+4
source share
4 answers

OnIdiom , like OnPlatform is an object that you must declare. In your case, you set the OnIdiom.Phone property to OnIdiom.Phone object that does not have one.

Your Xaml should look more:

 <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> 
+17
source

Xamarin.Forms example:

 <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> 
+2
source

One option that works in code is to use

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

to overwrite existing Xaml definition. innerGrid is the name of the Grid used in Xaml.

0
source

A much nicer syntax than in Xamarin.Forms v3.2 (using the new built-in XAML extensions):

 <Grid.ColumnDefinitions> <ColumnDefinition Width="{OnIdiom Phone= {OnPlatform iOS=*, Android=*, UWP=100 }, Tablet= {OnPlatform iOS=*, Android=*, UWP=200 }}"> </ColumnDefinition> </Grid.ColumnDefinitions> 
0
source

Source: https://habr.com/ru/post/1270468/


All Articles