I want to use Grid
layout type to define a small square in the lower corner of my phone screen.
I'm trying to do it by getting a column width in the OnAppearing
method in the code-behind, and using that to set a row height.
In Xaml I've done this:
<Grid x:Name ="iconGrid"
BackgroundColor="Green">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="9*" />
<ColumnDefinition Width="1.9*" />
<ColumnDefinition Width="0.1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="4.9*" />
<RowDefinition Height="0.1*" />
</Grid.RowDefinitions>
<Frame BackgroundColor="Purple"
Grid.Row ="1"
Grid.Column ="1" />
</Grid>
And then in OnAppearing I've done this:
protected override async void OnAppearing()
{
base.OnAppearing();
iconGrid.RowDefinitions[1].Height = new GridLength(iconGrid.ColumnDefinitions[1].Width.Value);
iconGrid.RowDefinitions[2].Height = new GridLength(iconGrid.ColumnDefinitions[2].Width.Value);
var remainingHeight = iconGrid.Height - iconGrid.RowDefinitions[2].Height.Value - iconGrid.RowDefinitions[2].Height.Value;
iconGrid.RowDefinitions[0].Height = new GridLength(remainingHeight);
Debug.WriteLine("rows: " + iconGrid.RowDefinitions[0].Height + ", " + iconGrid.RowDefinitions[1].Height + ", " + iconGrid.RowDefinitions[2].Height);
}
My debug output is:
rows: 811.8.Absolute, 1.9.Absolute, 0.1.Absolute
So from the debug output it looks like I'm just getting the star values of the column widths and using them literally as absolute values for the rows, which of course doesn't work.
What I want is to get the on-screen absolute values for the columns and use them as absolute values for the rows.
So in short I need to get the actual on-screen point sise of columns that were defined as star-types. Is this possible? It seems like it must be.
Think I got lost in all your wants and lost myself too.. Sorry, about that.
Getting absolute values from displays is well.. Complicated, but there is a new Nuget-package on the way which is still in Pre-release, called Xamarin.Essentials that will enable you to get the width and height of the phone and based on your design it should be pretty easy to figure out the size you need.
To install Nuget package Xamarin.Essential, start by ticking the checkbox: Include prerelease.
Add to code-behind:
using Xamarin.Essentials;
You can acquire orientation, rotation, densite, width and height, and the last doesn't change with rotation - if Genymotions Android emulator can be believed that is.
var metrics = DeviceDisplay.ScreenMetrics;
// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = metrics.Orientation;
// Rotation (0, 90, 180, 270)
var rotation = metrics.Rotation;
// Width (in pixels)
var width = metrics.Width;
// Height (in pixels)
var height = metrics.Height;
// Screen density
var density = metrics.Density;
From what I can tell it gives you available height of the page, that is with the height of any menu on top subtracted.
More information about it here: https://learn.microsoft.com/en-us/xamarin/essentials/?context=xamarin/xamarin-forms
--- Original Post "Relative Layout" ---
<RowDefinition
RelativeLayout.HeightConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Height,
Factor=0.1,
Constant=0}" />
<ColumnDefinition
RelativeLayout.WidthConstraint="{ConstraintExpression
Type=RelativeToParent,
Property=Heigth,
Factor=0.1,
Constant=0}"