Xamarin 表单按钮绑定
Xamarin Forms Button Binding
我正在尝试将我的按钮绑定到视图模型中的命令,但是当我单击它时它不会触发:
<?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:sys="clr-namespace:System;assembly=mscorlib"
x:Class="MyNamespace.UI.Views.AuthenticationPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Text="Authenticate" Command="{Binding AuthenticateCommand}" Grid.Row="0"/>
<Label Text="Locked" Grid.Row="0"/>
</Grid>
</ContentPage>
后端代码:
public partial class AuthenticationPage : ContentPage
{
public AuthenticationPage()
{
InitializeComponent();
this.BindingContext = new AuthenticationViewModel(this);
}
protected override bool OnBackButtonPressed()
{
return false;
}
}
我的视图模型:
public class AuthenticationViewModel
{
private ContentPage contentPage;
public ICommand AuthenticateCommand { get; set; }
public AuthenticationViewModel(ContentPage contentPage)
{
this.contentPage = contentPage;
AuthenticateCommand = new Command(test, () => true);
}
private void test()
{
}
}
我以前让它工作,但在做了一些更改后它停止工作了。我认为我不需要 INotifyPropertyChanged
按钮命令,对吧?
我认为这是因为您的 Label 与按钮在同一行并且与它重叠,所以 click/touch 根本没有到达按钮。是的,您 不需要 通知命令的 属性 更改,只要您在构造函数中初始化它/在绑定发生之前。
尝试
<?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:sys="clr-namespace:System;assembly=mscorlib"
x:Class="MyNamespace.UI.Views.AuthenticationPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Text="Authenticate" Command="{Binding AuthenticateCommand}" Grid.Row="0"/>
<Label Text="Locked" Grid.Row="1"/>
</Grid>
</ContentPage>
我正在尝试将我的按钮绑定到视图模型中的命令,但是当我单击它时它不会触发:
<?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:sys="clr-namespace:System;assembly=mscorlib"
x:Class="MyNamespace.UI.Views.AuthenticationPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Text="Authenticate" Command="{Binding AuthenticateCommand}" Grid.Row="0"/>
<Label Text="Locked" Grid.Row="0"/>
</Grid>
</ContentPage>
后端代码:
public partial class AuthenticationPage : ContentPage
{
public AuthenticationPage()
{
InitializeComponent();
this.BindingContext = new AuthenticationViewModel(this);
}
protected override bool OnBackButtonPressed()
{
return false;
}
}
我的视图模型:
public class AuthenticationViewModel
{
private ContentPage contentPage;
public ICommand AuthenticateCommand { get; set; }
public AuthenticationViewModel(ContentPage contentPage)
{
this.contentPage = contentPage;
AuthenticateCommand = new Command(test, () => true);
}
private void test()
{
}
}
我以前让它工作,但在做了一些更改后它停止工作了。我认为我不需要 INotifyPropertyChanged
按钮命令,对吧?
我认为这是因为您的 Label 与按钮在同一行并且与它重叠,所以 click/touch 根本没有到达按钮。是的,您 不需要 通知命令的 属性 更改,只要您在构造函数中初始化它/在绑定发生之前。
尝试
<?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:sys="clr-namespace:System;assembly=mscorlib"
x:Class="MyNamespace.UI.Views.AuthenticationPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Text="Authenticate" Command="{Binding AuthenticateCommand}" Grid.Row="0"/>
<Label Text="Locked" Grid.Row="1"/>
</Grid>
</ContentPage>