how to use Idioms in xamarin.forms using C# code and XAML

c#, xamarin, xamarin.forms, xamarin.ios, xaml

Solution

You can use it with virtually any property of any type of object you use in XAML.

Just use the right property and get the correct type of the argument it needs.

For instance, if you want to set the `Spacing` in a `Grid`, just do this:

<Grid VerticalOptions="FillAndExpand">
  <Grid.ColumnSpacing>
    <OnIdiom x:TypeArguments="x:Double"
             Phone="20"
             Tablet="40"/>
 </Grid.ColumnSpacing>
  <Grid.RowSpacing>
    <OnIdiom x:TypeArguments="x:Double"
             Phone="10"
             Tablet="20"/>
  </Grid.RowSpacing>
  <Grid.Padding>
    <OnIdiom x:TypeArguments="Thickness"
             Phone="10, 10, 10, 0"
             Tablet="20, 20, 20, 0"/>
  </Grid.Padding>
  <!-- Grid Content -->
</Grid>

The things to note here is that we set the `ColumnSpacing` by adding a child node to the `Grid` and as a child of that we use the `OnIdiom`. If you would want to do something different for a platform there is also the OnPlatform.

The only thing you need to figure out is what the `x:TypeArguments` has to be. This is the type of object that you are trying to assign as a value. In the case above, you would have to check what the type of `Grid.ColumnSpacing` is, which is a `Double`.

For more on this check this blog post by James Montemagno.

Problem

How to use idiom to set Grid (height, width) padding, margin and to set label font size in xamarin forms using C# and XAML code. I have an example of use Rectangle with StackLayout. But i am not aware of using it with other controls. ``` <StackLayout Spacing="10" AbsoluteLayout.LayoutFlags="All" BackgroundColor="#0E517B" Padding="0,30,0,0"> <StackLayout.AbsoluteLayout.LayoutBounds> <OnIdiom x:TypeArguments="Rectangle" Phone="0.5,1,1,0.80" Tablet="1,0,0.5,1" /> </StackLayout.AbsoluteLayout.LayoutBounds></StackLayout> ```

Original source