Me.Toolkit.Maui.Sizing
26.9.26
See the version list below for details.
dotnet add package Me.Toolkit.Maui.Sizing --version 26.9.26
NuGet\Install-Package Me.Toolkit.Maui.Sizing -Version 26.9.26
<PackageReference Include="Me.Toolkit.Maui.Sizing" Version="26.9.26" />
<PackageVersion Include="Me.Toolkit.Maui.Sizing" Version="26.9.26" />
<PackageReference Include="Me.Toolkit.Maui.Sizing" />
paket add Me.Toolkit.Maui.Sizing --version 26.9.26
#r "nuget: Me.Toolkit.Maui.Sizing, 26.9.26"
#:package Me.Toolkit.Maui.Sizing@26.9.26
#addin nuget:?package=Me.Toolkit.Maui.Sizing&version=26.9.26
#tool nuget:?package=Me.Toolkit.Maui.Sizing&version=26.9.26
Me.Toolkit.Maui.Sizing
Relative sizes for .NET MAUI: a percentage of the parent, the window, the display or another
element, recomputed as it changes — the way CSS %, vw and vh work.
MAUI sizes are absolute numbers. WidthRequest="100" is the same 100 on a phone and on a 4K
monitor, and OnIdiom only lets you pick a different absolute number per device. This adds a
relative one.
Use
<ContentPage xmlns:me="https://github.com/melihercan/Me.Toolkit.Maui" ...>
<Image WidthRequest="{me:Relative 30}" />
<Label FontSize="{me:Relative 3, To=Window, Min=12, Max=40}" />
<Image WidthRequest="{me:Relative 15, To=Display, Axis=Shorter}"
HeightRequest="{me:Relative 15, To=Display, Axis=Shorter}" />
<BoxView WidthRequest="{me:Relative 50, Source={x:Reference Card}}" />
<BoxView WidthRequest="{me:Relative 50, AncestorType={x:Type Border}}" />
No registration is needed. It works on any double property: WidthRequest, HeightRequest,
FontSize, Spacing, MaximumWidthRequest and so on.
| Option | Values | Default |
|---|---|---|
Percent (first argument) |
30 is 30% |
— |
Portrait, Landscape |
a different percentage per window orientation | Percent |
To |
Parent, Window, Display, Self |
Parent |
Offset |
added after the percentage, may be negative | 0 |
Axis |
Width, Height, Shorter, Longer |
Width |
Min, Max |
clamp the result | 0, no limit |
Source |
an element, usually {x:Reference} |
— |
AncestorType |
the nearest ancestor of this type | — |
From code:
label.SetRelativeSize(Label.FontSizeProperty,
new RelativeSize(3) { To = SizeReference.Window, Min = 12, Max = 40 });
Aspect ratios and gaps
To=Self measures the element itself, padding included, so a height can follow the element's own
width:
<Image WidthRequest="{me:Relative 50}" HeightRequest="{me:Relative 56.25, To=Self}" />
It must read the other dimension than the one it sets — HeightRequest from Width,
WidthRequest from Height — and anything else is refused, since it would chase its own value.
Offset is added after the percentage and before Min/Max, which is CSS's calc():
<HorizontalStackLayout Spacing="8">
<BoxView WidthRequest="{me:Relative 50, Offset=-4, AncestorType={x:Type VerticalStackLayout}}" />
<BoxView WidthRequest="{me:Relative 50, Offset=-4, AncestorType={x:Type VerticalStackLayout}}" />
</HorizontalStackLayout>
In styles
{me:Relative} cannot go in a Style setter — a setter is shared, and a relative size is tracked
per element. RelativeSizing's attached properties bridge the two:
<Style TargetType="Label">
<Setter Property="me:RelativeSizing.FontSize" Value="3, To=Window, Min=12, Max=40" />
</Style>
The value is text with the same option names as {me:Relative}: an optional leading percentage,
then Name=Value pairs. Numbers are read the same way in every culture. Source and AncestorType
cannot be written as text, since text cannot name an element or a type.
RelativeSizing.WidthRequest, HeightRequest and FontSize are available, and work directly on an
element too: <BoxView me:RelativeSizing.WidthRequest="25, Offset=10" />. FontSize finds the
property of whichever control it is on, including third-party ones with a FontSizeProperty.
Removing the style removes the size, and the property goes back to its default.
From code, element.ClearRelativeSize(property) does the same. Prefer it to RemoveBinding: MAUI
keeps the last value a removed binding set, for any binding, and ClearValue does not take it back.
Portrait and landscape
Rotation needs nothing special: it changes the window's size, so every relative size recomputes.
Axis=Shorter or Longer gives a size rotation does not change at all.
For a different percentage per orientation, name one or both:
<Image WidthRequest="{me:Relative Portrait=90, Landscape=45}" />
<Image WidthRequest="{me:Relative 50, Landscape=25}" />
Orientation is the shape of the element's window — landscape when it is wider than tall,
portrait otherwise, square included — whatever the size is measured against. A card is wider than
it is tall on a portrait phone too, so the reference's own shape would say "landscape" almost
everywhere. On a phone the window's shape is the screen's orientation; on a desktop, a tall narrow
window counts as portrait. Until the element is in a window, Percent applies.
With OnIdiom and OnPlatform
Put them inside, as a named argument:
<Image WidthRequest="{me:Relative Percent={OnIdiom Phone=80, Tablet=50, Desktop=30}}" />
Two other spellings look reasonable and do not work, both because of how MAUI compiles XAML:
{me:Relative {OnIdiom ...}}— as the unnamed first argument,OnIdiomis not told what type it is producing and throws "Cannot determine property to provide the value for". Naming itPercent=fixes that.{OnIdiom Desktop={me:Relative 30}, Default=100}— nested the other way,{me:Relative}cannot see the element it is sizing, and the compiled XAML applies the result withSetValue, which cannot carry a binding. This throws a message saying which way round to write it.
What it measures
- Parent is the parent's size less its padding — the space the element is actually laid
out in, as a CSS percentage refers to the content box.
SourceandAncestorTypeexclude padding the same way. ABorder's stroke is not subtracted. - Window is the window the element is in, and follows it as it is resized.
- Display is the main display in device-independent units, and changes on rotation. On a desktop with several monitors it is the main one, not necessarily the one the window is on.
Until the reference has a size — before the first layout, or before the element is in a window — the property keeps its default. If the element moves to another parent or window, it follows. Setting the property directly replaces the relative size, as it would any binding.
Things to know
{me:Relative}is not for aStylesetter — useRelativeSizing, above. It throws saying so.- Watch for layout loops. A percentage of a parent that sizes itself to its content —
Autorows, aVerticalStackLayoutin its stacking direction — makes the child depend on the parent and the parent on the child. Size against something with a size of its own: a*row, the window, or a named element. - No leaks through the display. The display's change event is static; an element subscribes only while it is in a window and unsubscribes when it leaves.
- Trim-safe. The binding is a
TypedBinding, not a string path, so iOS and Android Release builds do not trim away what it reads.
Documentation
Full documentation is in the wiki — Design Notes for why each library is shaped the way it is, Building and Testing for the multi-targeting rules, and Publishing for how these packages are released.
Source: github.com/melihercan/Me.Toolkit.Maui
Licence
MIT.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-android36.0 is compatible. net10.0-browser was computed. net10.0-ios was computed. net10.0-ios26.0 is compatible. net10.0-maccatalyst was computed. net10.0-maccatalyst26.0 is compatible. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net10.0-windows10.0.19041 is compatible. |
-
net10.0
- Microsoft.Maui.Controls (>= 10.0.101)
-
net10.0-android36.0
- Microsoft.Maui.Controls (>= 10.0.101)
-
net10.0-ios26.0
- Microsoft.Maui.Controls (>= 10.0.101)
-
net10.0-maccatalyst26.0
- Microsoft.Maui.Controls (>= 10.0.101)
-
net10.0-windows10.0.19041
- Microsoft.Maui.Controls (>= 10.0.101)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
First release.
{me:Relative} and SetRelativeSize size a double property as a percentage of the parent (less its padding), the window, the main display, an element named with x:Reference, or the nearest ancestor of a type. Width, Height, Shorter and Longer pick the dimension; Min and Max clamp the result; Portrait and Landscape give a different percentage for each orientation of the window. To=Self keeps an aspect ratio, Offset gives calc(50% - 8), and RelativeSizing's attached properties let a Style carry a relative size. The value is recomputed whenever the reference changes size or the element moves in the tree.