我正在尝试将我的按钮绑定到我的视图模型中的命令,但是当我点击它时它不会触发:
<?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
按钮命令,对吧?
答案 0 :(得分:0)
我认为这是因为您的Label与按钮位于同一行并且它与它重叠,因此点击/触摸根本无法到达按钮。是的,您不需要通知命令的属性更改,只要您在构造函数中/在绑定发生之前对其进行初始化。
尝试
<?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>