.NET MAUI for C# Developers by Jesse Liberty Rodrigo Juarez

.NET MAUI for C# Developers by Jesse Liberty Rodrigo Juarez

Author:Jesse Liberty, Rodrigo Juarez
Language: eng
Format: epub
Publisher: Packt Publishing Limited
Published: 2023-03-21T00:00:00+00:00


Grid

Nothing comes close to Grid for flexibility, although its basic use is dead simple. A grid consists of rows and columns. You define the size of each and then fill in the resulting boxes.

By default, all the columns are the same width, and all the rows are the same height. Rows and columns are identified (by default) by their offset starting at column 0, row 0. You can leave out the 0 (it is the default value) but I advise against doing so for readability. (This is the same reason I mark private methods and classes with the private keyword.)

We can recreate the LoginPage page using Grid. Let’s look at the first approximation in full (I’ve left out the resources section, as it is unchanged):

<?xml version="1.0" encoding="utf-8" ?> <ContentPage Title="Login Page" x:Class="ForgetMeNotDemo.View.LoginPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:behaviors="http://schemas.microsoft.com/dotnet /2022/maui/toolkit" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> <VerticalStackLayout x:Name="LoginStackLayout"> [1] <Grid [2] ColumnDefinitions="*,*,*" [3] RowDefinitions="*,*,*,*,*" [4] x:Name="LoginGrid"> <Label Grid.Column="0" [5] Grid.Row="0" [6] HorizontalOptions="End" [7] Margin="5,20,0,10" Text="User Name" VerticalOptions="Center" [8] /> <Entry Grid.Column="1" Grid.ColumnSpan="2" [9] Grid.Row="0" HorizontalOptions="Center" Margin="5,20,0,10" Placeholder="User Name" Text="{Binding Name}" VerticalOptions="End" WidthRequest="150" /> <Label [10] Grid.Column="0" Grid.Row="1" HorizontalOptions="End" Margin="5,10" Text="Password" VerticalOptions="Center" /> <Entry Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" HorizontalOptions="Center" IsPassword="True" Placeholder="Password" Text="{Binding Password}" VerticalOptions="Start" WidthRequest="150" /> <BoxView Color="Red" Grid.Column="0" Grid.ColumnSpan="3" [11] Grid.Row="2" HeightRequest="2" Margin="0,10" WidthRequest="400" />

The next thing to add is the frames:

<Frame BorderColor="Blue" CornerRadius="10" Grid.Column="0" Grid.Row="3" HasShadow="True" HeightRequest="50" WidthRequest="50"> <Frame.Background> <LinearGradientBrush EndPoint="1,0"> <GradientStop Color="Yellow" Offset="0.2" /> <GradientStop Color="Red" Offset="0.1" /> </LinearGradientBrush> </Frame.Background> </Frame> <Frame BorderColor="Blue" CornerRadius="10" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="3" HasShadow="True" HeightRequest="50" WidthRequest="100"> <Frame.Background> <RadialGradientBrush> <GradientStop Color="Yellow" Offset="0.2" /> <GradientStop Color="Red" Offset="0.1" /> </RadialGradientBrush> </Frame.Background> </Frame>

With that in place, we can add the three buttons and then close Grid and VerticalStackLayout:

<Button BackgroundColor="Gray" Command="{Binding SubmitCommand}" Grid.Column="0" Grid.Row="4" Margin="5" Text="Submit" /> <Button BackgroundColor="Gray" Command="{Binding CreateCommand}" Grid.Column="1" Grid.Row="4" Margin="5" Text="Create Account" /> <Button BackgroundColor="Gray" Clicked="OnForgotPassword" Grid.Column="2" Grid.Row="4" Margin="5" Text="Forgot Password" /> <Label Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="5" Text=" x:Name="CreateAccount" /> </Grid> </VerticalStackLayout> </ContentPage>

[1] We’ll put Grid inside a VerticalStackLayout so that we can add the ProgressBar below the grid (adding it to VerticalStackLayout’s Children collection, which will have only two members: Grid and ProgressBar).

[2] We declare Grid with the keyword.

[3] We declare three columns of equal size (*,*,*).

“Some have stars upon thars” – Dr. Seuss

Stars don’t make much difference when they are all the same size, but if, for example, we wanted the first to be twice as big as the others, we would write the following:

ColumnDefinitions="2*,*,*"

In that case, the column would be divided into four equal parts and the first column would get two of them and the other columns one each. The result is that the first column would be twice as wide as the others.

[4] Similarly, we declare five rows of equal size.

[5] We place the Label inside column 0.

[6] We place the Label inside row 0.

[7] The horizontal option is with respect to the column the control is in.

[8] The vertical option is with respect to the row the control is in.

[9] A control can span across more than one column. In this case, the entry begins at column 1 and runs for a column span of 2 (that is, it occupies both column 1 and 2).



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.