c# - 按钮命令绑定(bind)在 Xamarin.Forms 中不起作用

标签 c# xaml mvvm binding xamarin.forms

我想将命令绑定(bind)到按钮的命令属性。这看起来非常简单,因为我以前在 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>

代码隐藏

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

View 模型

public ICommand IncreaseCommand { get; private set; }
private int number;
public string numberText { get; private set;}

构造函数:

public ViewModel()
{
    IncreaseCommand = new Command (() => IncreaseExecuted ());
    number = 0;
    numberText = number.ToString ();
    OnPropertyChanged (numberText);
}

然后

private void IncreaseExecuted()
{
    number++;
    numberText = number.ToString ();
    OnPropertyChanged (numberText);
}

当我使用 Xamarin Android Player (KitKat) 运行应用程序时,我看到了按钮,标签读数为 0。然后我按下按钮,但没有任何反应。我尝试检查断点会发生什么,但应用程序不会暂停,即使它们在我的 ViewModel 的构造函数中也是如此。我想这与模拟器有关。无论如何,我认为绑定(bind)没问题,因为我可以在屏幕上看到一个“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));
        }
    }
}

也许当我调用 OnPropertyChanged 时,我的 numberText 属性没有得到更新?但我之前曾多次使用完全相同的 ViewModelBase 类,并且它始终运行良好。最后一件事,我的 CustomPage 页面被包裹在 NavigationPage 中,它是 TabbedPage 的子级:

MainPage.xaml.cs

this.Children.Add (new NavigationPage (new CustomPage ()) {Title="Something"} );

这应该不会影响任何事情,但以防万一。那么我的命令绑定(bind)有什么问题呢?提前致谢!

最佳答案

这个答案与原问题的问题没有直接关系,但是这个问题在搜索引擎中排名最高,这个问题的标题与这个答案回答的问题有歧义。

我在使用 Command 时遇到问题,它不会触发相关的命令操作。我的问题是我将 Command 定义为 field 而不是 property

作品:

public Command MyCommand { get; set; }

不起作用:

public Command MyCommand;

希望这对其他人有帮助。

关于c# - 按钮命令绑定(bind)在 Xamarin.Forms 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27460742/

相关文章:

c# - 使用 DirectorySearcher.FindAll() 时发生内存泄漏

c# - 为什么 ListView 只显示 12 个项目?

wpf - 在XAML中引用另一个项目: Undefined CLR namespace

c# - 单击按钮时调用两个命令-ViewModel中的一个在后面的代码中

.net - 将控件映射到 View 模型

c# - 如何将文件路径从我的代码中取出并放入 mysql 的插入语句中?

c# - Google Analytics API 相关产品

c# - 我可以在 XAML 中实现 ScrollToHorizo​​ntalOffset() 功能吗? (对于动态列表)

c# - Visual Studio 2015 XAML 设计器因 System.Exception 而失败

c# - 如何在 WinRT 的应用 ViewModel 中异步使用 json.net?