Grid Column Behavior: Xamarin XAML vs Micorsoft XAML
Join the DZone community and get the full member experience.
Join For Freethere’s a small detail difference in how rows and columns in a grid behave in xamarin forms vs microsoft xaml. a small difference but with quite a big impact. look at the following xaml snippet (microsoft xaml):
<grid background="red"> <grid.rowdefinitions> <rowdefinition height="50" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid grid.row="1" background="blue"> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="300" /> </grid.rowdefinitions> </grid> </grid>
at runtime this looks like:
exactly what we as seasoned xaml developers would expect.
converted to xamarin forms xaml this turns into:
<grid backgroundcolor="red"> <grid.rowdefinitions> <rowdefinition height="50" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid grid.row="1" backgroundcolor="blue"> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="300" /> </grid.rowdefinitions> </grid> </grid>
only difference is the property name to set the background color. however, if we look at this at runtime we see a completely different story.
it looks like the inner grid isn’t rendered at all. but let’s try to add a label to the inner grid.
<grid backgroundcolor="red"> <grid.rowdefinitions> <rowdefinition height="50" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid grid.row="1" backgroundcolor="blue"> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="300" /> </grid.rowdefinitions> <label text="hello xamarin!" /> </grid> </grid>
so what exactly is going on here? the inner grid (the blue one) doesn’t specify any columns, that means that by default there’s only one column available. in microsoft xaml the width of that default column is set to “*” so it takes up all available space. in xamarin forms that width is set to “auto” so it only takes up the space it needs. a subtle difference with a big impact.
to fix this, add a column with “*” as width:
<grid backgroundcolor="red"> <grid.rowdefinitions> <rowdefinition height="50" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="*" /> </grid.columndefinitions> <grid grid.row="1" backgroundcolor="blue"> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="300" /> </grid.rowdefinitions> </grid> </grid>
result:
conclusion
in this post i’ve shown a difference in implementation between xamarin forms xaml and microsoft xaml and how to fix it.
note that this sample is based on xamarin forms 1.2. xamarin has stated that they will change the implementation in forms 1.3 to reflect the microsoft implementation.
Published at DZone with permission of Nico Vermeir, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
10 Traits That Separate the Best Devs From the Crowd
-
From CPU to Memory: Techniques for Tracking Resource Consumption Over Time
-
Integrating AWS With Salesforce Using Terraform
-
Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
Comments