Я изучаю макет Xamarin и сталкиваюсь с приведенным ниже кодом:
<?xml version = "1.0" encoding = "utf-8" ?>
<ContentPage xmlns = "http://xamarin.com/schemas/2014/forms"
xmlns:x = "http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local = "clr-namespace:MyApp.Validators"
x:Class = "MyApp.MainPage">
<StackLayout HorizontalOptions = "Fill" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height = "*" ></RowDefinition>
<RowDefinition Height = "*"></RowDefinition>
<RowDefinition Height = "*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Row = "0" Grid.Column = "0">
<Grid.RowDefinitions>
<RowDefinition Height = "*" ></RowDefinition>
<RowDefinition Height = "*"></RowDefinition>
<RowDefinition Height = "*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Entry x:Name = "Username" Grid.Row = "0" Placeholder = "Username" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:UsernameValidatorBehavior></local:UsernameValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Entry x:Name = "Password" Grid.Row = "1" Keyboard = "Numeric" Placeholder = "Password" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:PasswordValidatorBehavior></local:PasswordValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Entry x:Name = "Email" Grid.Row = "2" Placeholder = "Email" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:EmailValidatorBehavior></local:EmailValidatorBehavior>
</Entry.Behaviors>
</Entry>
</Grid>
<Grid VerticalOptions = "End" Grid.Row = "1" Grid.Column = "0">
<Grid.RowDefinitions>
<RowDefinition Height = "Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "5*"></ColumnDefinition>
<ColumnDefinition Width = "5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name = "Submit" Grid.Row = "0" Grid.Column = "0" Text = "Submit" Clicked = "OnButtonClicked"></Button>
<Button x:Name = "Cancel" Grid.Row = "0" Grid.Column = "1" Text = "Cancel" Clicked = "Cancel_Clicked"></Button>
</Grid>
</Grid>
</StackLayout>
</ContentPage>
Мой вопрос в том, что я пытался использовать
<Grid VerticalOptions = "End" Grid.Row = "1" Grid.Column = "0">
чтобы две кнопки располагались внизу экрана, но вместо этого они отображались в середине экрана. Как решить эту проблему?
ОБНОВЛЕНИЕ1
Я изменил сетку, и теперь xaml выглядит следующим образом:
<?xml version = "1.0" encoding = "utf-8" ?>
<ContentPage xmlns = "http://xamarin.com/schemas/2014/forms"
xmlns:x = "http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local = "clr-namespace:MyApp.Validators"
x:Class = "MyApp.MainPage">
<StackLayout HorizontalOptions = "Fill" >
<Grid VerticalOptions = "End">
<Grid.RowDefinitions>
<RowDefinition Height = "*" ></RowDefinition>
<RowDefinition Height = "*"></RowDefinition>
<RowDefinition Height = "*"></RowDefinition>
<RowDefinition Height = "*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*"></ColumnDefinition>
<ColumnDefinition Width = "*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Entry x:Name = "Username" Grid.Row = "0" Grid.ColumnSpan = "2" Placeholder = "Username" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:UsernameValidatorBehavior></local:UsernameValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Entry x:Name = "Password" Grid.Row = "1" Grid.ColumnSpan = "2" Keyboard = "Numeric" Placeholder = "Password" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:PasswordValidatorBehavior></local:PasswordValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Entry x:Name = "Email" Grid.Row = "2" Grid.ColumnSpan = "2" Placeholder = "Email" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:EmailValidatorBehavior></local:EmailValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Button x:Name = "Submit" Grid.Row = "3" Grid.Column = "0" VerticalOptions = "End" Text = "Submit" Clicked = "OnButtonClicked"></Button>
<Button x:Name = "Cancel" Grid.Row = "3" Grid.Column = "1"
VerticalOptions = "End" Text = "Cancel" Clicked = "Cancel_Clicked"></Button>
</Grid>
</StackLayout>
</ContentPage>
Прямо сейчас есть только одна сетка с 4 строками и 2 столбцами. я добавил
VerticalOptions = "End"
inside two buttons.
Но кнопок по-прежнему нет внизу экрана и более того, становится еще хуже.
У вас есть StackLayout, содержащий одну сетку, которая содержит 2 другие сетки. Это действительно сложный макет для простой страницы. Попробуйте заменить все это одной сеткой с 4 строками и двумя столбцами. Элементы управления вводом могут быть первыми тремя строками с colspan=2, тогда ваши кнопки могут быть 2 столбцами в последней строке.
@Jason Я сделал, но кнопки все еще не внизу экрана
У вас есть для их VerticalOptions значение «Конец»?
@Jason Да, пожалуйста, ознакомьтесь с Update1
Избавьтесь от StackLayout
@ Джейсон Спасибо, это работает!
Приведенный ниже код будет работать идеально для вас, добавьте VerticalOptions="FillAndExpand" в сетку и укажите высоту в процентах.
<?xml version = "1.0" encoding = "utf-8" ?>
<ContentPage xmlns = "http://xamarin.com/schemas/2014/forms"
xmlns:x = "http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local = "clr-namespace:MyApp.Validators"
x:Class = "MyApp.MainPage">
<StackLayout HorizontalOptions = "Fill">
<Grid VerticalOptions = "FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height = "1*" />
<RowDefinition Height = "1*" />
<RowDefinition Height = "1*" />
<RowDefinition Height = "7*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*"></ColumnDefinition>
<ColumnDefinition Width = "*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Entry x:Name = "Username" Grid.Row = "0" Grid.ColumnSpan = "2" Placeholder = "Username" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:UsernameValidatorBehavior></local:UsernameValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Entry x:Name = "Password" Grid.Row = "1" Grid.ColumnSpan = "2" Keyboard = "Numeric" Placeholder = "Password" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:PasswordValidatorBehavior></local:PasswordValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Entry x:Name = "Email" Grid.Row = "2" Grid.ColumnSpan = "2" Placeholder = "Email" PlaceholderColor = "Blue">
<Entry.Behaviors>
<local:EmailValidatorBehavior></local:EmailValidatorBehavior>
</Entry.Behaviors>
</Entry>
<Button x:Name = "Submit" Grid.Row = "3" Grid.Column = "0" VerticalOptions = "EndAndExpand" Text = "Submit" Clicked = "OnButtonClicked"></Button>
<Button x:Name = "Cancel" Grid.Row = "3" Grid.Column = "1"
VerticalOptions = "EndAndExpand" Text = "Cancel" Clicked = "Cancel_Clicked"></Button>
</Grid>
</StackLayout>
</ContentPage>
У вас есть три строки, индекс строки для кнопок должен быть
Gird.Row=2
, а неGrid.Row=1