按钮命令绑定在Xamarin.Forms中不起作用

Gáb*_*kás 9 c# xaml binding mvvm xamarin.forms

我想将命令绑定到我的按钮的命令属性.这似乎非常简单,因为我之前在WPF中已经多次这样做了,这里的方法非常相似.让我展示一些代码片段.

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" 
         x:Class="MyApp.View.CustomPage"
         Title="Something">

<ContentPage.Content>
    <StackLayout>
        <Button x:Name="numBtn" Text="Increase number" Command="{Binding IncreaseCommand}" />
        <Label x:Name="numLabel" Text="{Binding numberText}" />
    </StackLayout>
</ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

代码隐藏

public partial class CustomPage : ContentPage
{   
    public CustomPage ()
    {
        InitializeComponent ();
        BindingContext = ViewModelLocator.ViewModel();  //ViewModelLocator is singleton, gives
                                                        //you a ViewModel instance
    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型

public ICommand IncreaseCommand { get; private set; }
private int number;
public string numberText { get; private set;}
Run Code Online (Sandbox Code Playgroud)

构造函数:

public ViewModel()
{
    IncreaseCommand = new Command (() => IncreaseExecuted ());
    number = 0;
    numberText = number.ToString ();
    OnPropertyChanged (numberText);
}
Run Code Online (Sandbox Code Playgroud)

然后

private void IncreaseExecuted()
{
    number++;
    numberText = number.ToString ();
    OnPropertyChanged (numberText);
}
Run Code Online (Sandbox Code Playgroud)

当我使用Xamarin Android Player(KitKat)运行应用程序时,我看到按钮,标签读数为0.然后我按下按钮,没有任何反应.我尝试检查断点会发生什么,但应用程序不会暂停,即使它们在我的ViewModel的构造函数中也没有.我想这与模拟器有关.无论如何,我认为绑定是好的,因为我可以在屏幕上看到"0".可能是什么问题呢?让我展示我的ViewModelBase类以防万一:

ViewModelBase

public abstract class ViewModelBase : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

也许我numberText打电话时我的房产没有更新OnPropertyChanged?但是之前我曾多次使用完全相同的ViewModelBase类,它总能正常工作.最后一件事,我的CustomPage页面被包裹在NavigationPage一个孩子中TabbedPage:

MainPage.xaml.cs中

this.Children.Add (new NavigationPage (new CustomPage ()) {Title="Something"} );
Run Code Online (Sandbox Code Playgroud)

这不应该影响任何事情,只是以防万一.那我的命令绑定有什么问题?先感谢您!

And*_*per 6

你快到了.仔细看看你对OnPropertyChanged方法的调用; 您传递的是numberText的值,而不是名称.如果您更改代码以传递"numberText",我希望它能够正常工作.

编辑:我应该补充说,构造函数中的OnPropertyChanged调用具有相同的问题.在启动时看到"0"的原因是视图只是使用现有绑定来检索值.

编辑2:现在Xamarin支持C#6.0,您可以使用新的"nameof"表达式,无需使用硬编码字符串.或者,您可以使用MvvmCross,MvvmLight或XLabs的MVVM类.


Hei*_*tad 5

该答案与原始问题的问题没有直接关系,但是,该问题是搜索引擎中排名最高的问题,并且该问题的标题与该答案所回答的问题不明确。

我遇到了Command无法触发相关命令操作的问题。我的问题是我将定义Commandfield而不是property

作品:

public Command MyCommand { get; set; }
Run Code Online (Sandbox Code Playgroud)

不起作用:

public Command MyCommand;
Run Code Online (Sandbox Code Playgroud)

希望这对其他人有帮助。