ICommand 命令在 Maui Preview 11 中不起作用
ICommand Command not working in Maui Preview 11
我是 Maui 的新手,正在按照 Xamarin 表单教程设计登录页面。我似乎无法通过登录页面,因为按钮的绑定没有触发。
我在输出 window 中得到了这个,但无法在任何地方找到任何修复。
该应用程序似乎 运行 很好,只是两个按钮不响应用户交互。
[ViewRootImpl@ae55060[MainActivity]] MSG_RESIZED: frame=[0,0][800,1280] ci=[0,32][0,0] vi=[0,32][0,426] or=1
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.Border is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.Border from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.Label is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.Label from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
Login.xaml:
<StackLayout>
<StackLayout> Image and Label over here </StackLayout>
<StackLayout Margin="10" Padding="20" VerticalOptions="EndAndExpand">
<Border Stroke="#620774"
StrokeThickness="4"
Padding="16,8"
BackgroundColor="Transparent"
HorizontalOptions="Center">
<Border.StrokeShape>
<RoundRectangle CornerRadius="5,5,5,5" BackgroundColor="Transparent"/>
</Border.StrokeShape>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Email Address-->
<Image
GridLayout.Column="0"
GridLayout.Row="0"
Source="email.png"
Aspect="AspectFit"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
WidthRequest="20"
HeightRequest="35"/>
<Entry
GridLayout.Column="1"
GridLayout.Row="0"
PlaceholderColor="#9d2888"
Margin="4,0,0,0"
TextColor="Black"
HeightRequest="50"
Placeholder="Email Address"
Keyboard="Email"/>
<!--Password-->
<Image Source="key.png"
Aspect="AspectFit"
GridLayout.Column="0"
GridLayout.Row="1"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
WidthRequest="20"
HeightRequest="35"/>
<Entry
GridLayout.Column="1"
GridLayout.Row="1"
HeightRequest="50"
Margin="4,0,0,0"
PlaceholderColor="#9d2888"
TextColor="Black"
Placeholder="Password"
IsPassword="True"/>
</Grid>
</Border>
<Label Text="Forgot Password?"
HorizontalOptions="Center"
Margin="0,15,0,15"
VerticalOptions="StartAndExpand"
TextColor="#0052FF">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ForgotPassword}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
<StackLayout>
<Button Text="LOGIN"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
Margin="2"
BackgroundColor="#620774"
Command="{Binding LoginCommand}"/>
<Button Text="Dont have an account? Register here!"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
BackgroundColor="#620774"
Margin="2"
Command="{Binding RegisterCommand}"/>
</StackLayout>
<StackLayout> 2 Labels here </StackLayout>
</StackLayout>
Login.xaml.cs
public partial class Login : ContentPage
{
public Login()
{
InitializeComponent();
BindingContext = new LoginViewModel();
}
}
LoginViewModel.cs
public class LoginViewModel : BaseViewModel
{
//private fields
private string _username;
private string _password;
public string Username
{
get { return _username; }
set { _username = value; OnPropertyChanged(); }
}
public string Password
{
get { return _password; }
set { _password = value; OnPropertyChanged(); }
}
//Commands
public ICommand LoginCommand { get; set; }
public ICommand RegisterCommand { get; set; }
public ICommand ForgotPassword { get; set; }
public LoginViewModel()
{
System.Console.WriteLine("INSIDE LOGIN VIEWMODEL");
LoginCommand = new Command(UserLogin);
RegisterCommand = new Command(NewUser);
ForgotPassword = new Command(ResetPassword);
}
public async void UserLogin()
{
}
}
通过进行 2 处更改,我能够使其正常工作。但是,这些更改会影响页面的垂直间距。看看这是否适合您,然后尝试找到其他方法来获得所需的布局。
从 StackLayout
中删除 VerticalOptions="EndAndExpand"
。
从 Label
中删除 VerticalOptions="StartAndExpand"
。
Login.xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiTests.Login"
Title="Login"
BackgroundColor="White">
<StackLayout>
<Button Text="LOGIN"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
Margin="2"
BackgroundColor="#620774"
Command="{Binding LoginCommand}"/>
<!-- VerticalOptions="EndAndExpand" -->
<StackLayout Margin="10" Padding="20">
<Border Stroke="#620774"
StrokeThickness="4"
Padding="16,8"
BackgroundColor="Transparent"
HorizontalOptions="Center">
<Border.StrokeShape>
<RoundRectangle CornerRadius="5,5,5,5" BackgroundColor="Transparent"/>
</Border.StrokeShape>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Email Address-->
<Entry
GridLayout.Column="1"
GridLayout.Row="0"
PlaceholderColor="#9d2888"
Margin="4,0,0,0"
TextColor="Black"
HeightRequest="50"
Placeholder="Email Address"
Keyboard="Email"/>
<!--Password-->
<Entry
GridLayout.Column="1"
GridLayout.Row="1"
HeightRequest="50"
Margin="4,0,0,0"
PlaceholderColor="#9d2888"
TextColor="Black"
Placeholder="Password"
IsPassword="True"/>
</Grid>
</Border>
<!-- VerticalOptions="StartAndExpand" -->
<Label Text="Forgot Password?"
HorizontalOptions="Center"
Margin="0,15,0,15"
TextColor="#0052FF">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ForgotPassword}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
<Button Text="Dont have an account? Register here!"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
BackgroundColor="#620774"
Margin="2"
Command="{Binding RegisterCommand}"/>
</StackLayout>
</ContentPage>
我是 Maui 的新手,正在按照 Xamarin 表单教程设计登录页面。我似乎无法通过登录页面,因为按钮的绑定没有触发。
我在输出 window 中得到了这个,但无法在任何地方找到任何修复。
该应用程序似乎 运行 很好,只是两个按钮不响应用户交互。
[ViewRootImpl@ae55060[MainActivity]] MSG_RESIZED: frame=[0,0][800,1280] ci=[0,32][0,0] vi=[0,32][0,426] or=1
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.StackLayout is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.StackLayout from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.Border is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.Border from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
[0:] Microsoft.Maui.Controls.Element: Warning: Microsoft.Maui.Controls.Label is already a child of Microsoft.Maui.Controls.GridLayout. Remove Microsoft.Maui.Controls.Label from Microsoft.Maui.Controls.GridLayout before adding to Microsoft.Maui.Controls.GridLayout.
Login.xaml:
<StackLayout>
<StackLayout> Image and Label over here </StackLayout>
<StackLayout Margin="10" Padding="20" VerticalOptions="EndAndExpand">
<Border Stroke="#620774"
StrokeThickness="4"
Padding="16,8"
BackgroundColor="Transparent"
HorizontalOptions="Center">
<Border.StrokeShape>
<RoundRectangle CornerRadius="5,5,5,5" BackgroundColor="Transparent"/>
</Border.StrokeShape>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Email Address-->
<Image
GridLayout.Column="0"
GridLayout.Row="0"
Source="email.png"
Aspect="AspectFit"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
WidthRequest="20"
HeightRequest="35"/>
<Entry
GridLayout.Column="1"
GridLayout.Row="0"
PlaceholderColor="#9d2888"
Margin="4,0,0,0"
TextColor="Black"
HeightRequest="50"
Placeholder="Email Address"
Keyboard="Email"/>
<!--Password-->
<Image Source="key.png"
Aspect="AspectFit"
GridLayout.Column="0"
GridLayout.Row="1"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
WidthRequest="20"
HeightRequest="35"/>
<Entry
GridLayout.Column="1"
GridLayout.Row="1"
HeightRequest="50"
Margin="4,0,0,0"
PlaceholderColor="#9d2888"
TextColor="Black"
Placeholder="Password"
IsPassword="True"/>
</Grid>
</Border>
<Label Text="Forgot Password?"
HorizontalOptions="Center"
Margin="0,15,0,15"
VerticalOptions="StartAndExpand"
TextColor="#0052FF">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ForgotPassword}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
<StackLayout>
<Button Text="LOGIN"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
Margin="2"
BackgroundColor="#620774"
Command="{Binding LoginCommand}"/>
<Button Text="Dont have an account? Register here!"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
BackgroundColor="#620774"
Margin="2"
Command="{Binding RegisterCommand}"/>
</StackLayout>
<StackLayout> 2 Labels here </StackLayout>
</StackLayout>
Login.xaml.cs
public partial class Login : ContentPage
{
public Login()
{
InitializeComponent();
BindingContext = new LoginViewModel();
}
}
LoginViewModel.cs
public class LoginViewModel : BaseViewModel
{
//private fields
private string _username;
private string _password;
public string Username
{
get { return _username; }
set { _username = value; OnPropertyChanged(); }
}
public string Password
{
get { return _password; }
set { _password = value; OnPropertyChanged(); }
}
//Commands
public ICommand LoginCommand { get; set; }
public ICommand RegisterCommand { get; set; }
public ICommand ForgotPassword { get; set; }
public LoginViewModel()
{
System.Console.WriteLine("INSIDE LOGIN VIEWMODEL");
LoginCommand = new Command(UserLogin);
RegisterCommand = new Command(NewUser);
ForgotPassword = new Command(ResetPassword);
}
public async void UserLogin()
{
}
}
通过进行 2 处更改,我能够使其正常工作。但是,这些更改会影响页面的垂直间距。看看这是否适合您,然后尝试找到其他方法来获得所需的布局。
从
StackLayout
中删除VerticalOptions="EndAndExpand"
。从
Label
中删除VerticalOptions="StartAndExpand"
。
Login.xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiTests.Login"
Title="Login"
BackgroundColor="White">
<StackLayout>
<Button Text="LOGIN"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
Margin="2"
BackgroundColor="#620774"
Command="{Binding LoginCommand}"/>
<!-- VerticalOptions="EndAndExpand" -->
<StackLayout Margin="10" Padding="20">
<Border Stroke="#620774"
StrokeThickness="4"
Padding="16,8"
BackgroundColor="Transparent"
HorizontalOptions="Center">
<Border.StrokeShape>
<RoundRectangle CornerRadius="5,5,5,5" BackgroundColor="Transparent"/>
</Border.StrokeShape>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Email Address-->
<Entry
GridLayout.Column="1"
GridLayout.Row="0"
PlaceholderColor="#9d2888"
Margin="4,0,0,0"
TextColor="Black"
HeightRequest="50"
Placeholder="Email Address"
Keyboard="Email"/>
<!--Password-->
<Entry
GridLayout.Column="1"
GridLayout.Row="1"
HeightRequest="50"
Margin="4,0,0,0"
PlaceholderColor="#9d2888"
TextColor="Black"
Placeholder="Password"
IsPassword="True"/>
</Grid>
</Border>
<!-- VerticalOptions="StartAndExpand" -->
<Label Text="Forgot Password?"
HorizontalOptions="Center"
Margin="0,15,0,15"
TextColor="#0052FF">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ForgotPassword}"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
<Button Text="Dont have an account? Register here!"
MaximumHeightRequest="45"
CornerRadius="10"
HeightRequest="45"
TextColor="White"
BackgroundColor="#620774"
Margin="2"
Command="{Binding RegisterCommand}"/>
</StackLayout>
</ContentPage>