The prototype is meant to show some responsive ui samples along with a few markup extensions which you can incorporate to have similar functionality in your app.
The OnScreenSize
markup extension in the MauiMarkupExtension
namespaces simplifies creatinig responsive layouts based on pre-defined breakpoints
for each screen size.
-
Include the Extension in Your Project:
-
Copy the
OnScreenSizeExtension.cs
andOnScreenSizeSource.cs
file into your MAUI project. -
Breakpoints
(required) These are app window dimensions, not the device dimensons.- ExtraLarge : Monitors
- Large: Full-size Desktop, Bigger Tablets
- Medium: Resized Desktop screen (medium), Medium sized tablets
- Small: Phone
- ExtraSmall: Smart watch.
-
PageWidth
(required)- Send resize (Width) updates of Page and View to markup extension to calculate required value of control.
- declare a name on the Page or View
x:Name="Name"
and use accordingly. It is an attached proprty so add it to the ContentPage or any other control/Viewlocal:MyControl.PageWidth="{Binding Width, Source={Reference Name}}"
- declare a name on the Page or View
- Send resize (Width) updates of Page and View to markup extension to calculate required value of control.
-
TypeConverter
(optional)- Convert string value to required type, if needed.
-
-
Usage in XAML:
-
In your XAML file, add the namespace where you've placed the extension:
xmlns:extension="clr-namespace:namespace.Extensions" x:Name="MonkeyPage"
-
Utilize the extension in your
Grid
definitions:<Grid ColumnDefinitions="{extension:OnScreenSize Default='*', ExtraLarge='400', Large='400', Medium='250', Small='*', ExtraSmall='*', TypeConverter={x:Type ColumnDefinitionCollectionTypeConverter}}" RowSpacing="16"> <!-- Your layout content goes here --> </Grid>
You don't have to specify values for all breakpoints, there is a fallback login which works as following:
- If no value specified,
Default
value will be assigned for all screen sizes. - Specify a value for a
breakpoint
, the bigger screen size will fallback to that value, unless specified. example:With the above, theIsVisible="{extension:OnScreenSize 'False', Medium='True', ExtraSmall='False'}"
Large
andExtraLarge
breakpoint will take the value forMedium
. Similarly,Small
will fallback toExtraSmall
value.
- If no value specified,
-
Customize a
Control
for each screen size as needed. -
Specify a
TypeConverter
if needed to convert a string to required type, as done above. -
Your can use it for all UI controls.
-