2016-03-02 134 views
0

我想我的按钮绑定到我的命令,我视图模型,但是当我点击它,它不会触发: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: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

你确定这是Xamarin?看起来更像WPF给我。 – Eminem

+0

是的Eminem,它是Xamarin形式。 – Darius

回答

0

我认为这是因为你的标签与按钮位于同一行,并且它与其重叠,所以点击/触摸根本无法到达按钮。是的,只要你在绑定发生之前在构造函数中初始化它,你就需要通知该命令的属性更改。

尝试

<?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>