问题 xamarin.forms从xaml绑定到property


我是一个在xaml中绑定的新手,我有时候真的不明白。

我在我的xaml中有这个:

<ActivityIndicator IsRunning="{Binding IsLoading}" IsVisible="{Binding IsLoading}" />

绑定“IsLoading”。我在哪里声明/设置这个属性?!

我的.cs看起来像这样:

....
    public bool IsLoading;

    public CardsListXaml ()
    {
        InitializeComponent ();
        IsLoading = true;
 ....

4431
2017-09-18 12:44


起源



答案:


绑定通常是从 BindingContext property(在其他实现中,调用此属性 DataContext)。这是 null 默认情况下(至少在XAML的其他实现中),因此您的视图无法找到指定的属性。

在您的情况下,您必须设置 BindingContext 财产 this

public CardsListXaml()
{
    InitializeComponent();
    BindingContext = this;
    IsLoading = true;
}

但是,仅靠这一点是不够的。您当前的解决方案没有实现一种机制来通知视图任何属性更改,因此您的视图必须实现 INotifyPropertyChanged。相反,我建议你实施 模型 - 视图 - 视图模型 模式,它不仅非常适合数据绑定,而且会产生更易于维护和可测试的代码库:

public class CardsListViewModel : INotifyPropertyChanged
{
    private bool isLoading;
    public bool IsLoading
    {
        get
        {
            return this.isLoading;
        }

        set
        {
            this.isLoading = value;
            RaisePropertyChanged("IsLoading");
        }
    }

    public CardsListViewModel()
    {
        IsLoading = true;
    }

    //the view will register to this event when the DataContext is set
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
} 

然后在你的代码隐藏构造函数中:

public CardsListView()
{
    InitializeComponent();
    BindingContext = new CardsListViewModel();
}

只是澄清一下, DataContext 沿着视觉树向下瀑布,因此 ActivityIndicator control将能够读取绑定中指定的属性。

编辑:Xamarin.Forms(以及Silverlight / WPF等......对不起,已经有一段时间了!)还提供了一个 SetBinding 方法(见 数据绑定 部分)。


13
2017-09-18 13:09



Xamarin.Forms  BindableObject没有 DataContext 属性,但是 BindingContext - Stephane Delcroix
谢谢(你的)信息! - James Wright


答案:


绑定通常是从 BindingContext property(在其他实现中,调用此属性 DataContext)。这是 null 默认情况下(至少在XAML的其他实现中),因此您的视图无法找到指定的属性。

在您的情况下,您必须设置 BindingContext 财产 this

public CardsListXaml()
{
    InitializeComponent();
    BindingContext = this;
    IsLoading = true;
}

但是,仅靠这一点是不够的。您当前的解决方案没有实现一种机制来通知视图任何属性更改,因此您的视图必须实现 INotifyPropertyChanged。相反,我建议你实施 模型 - 视图 - 视图模型 模式,它不仅非常适合数据绑定,而且会产生更易于维护和可测试的代码库:

public class CardsListViewModel : INotifyPropertyChanged
{
    private bool isLoading;
    public bool IsLoading
    {
        get
        {
            return this.isLoading;
        }

        set
        {
            this.isLoading = value;
            RaisePropertyChanged("IsLoading");
        }
    }

    public CardsListViewModel()
    {
        IsLoading = true;
    }

    //the view will register to this event when the DataContext is set
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
} 

然后在你的代码隐藏构造函数中:

public CardsListView()
{
    InitializeComponent();
    BindingContext = new CardsListViewModel();
}

只是澄清一下, DataContext 沿着视觉树向下瀑布,因此 ActivityIndicator control将能够读取绑定中指定的属性。

编辑:Xamarin.Forms(以及Silverlight / WPF等......对不起,已经有一段时间了!)还提供了一个 SetBinding 方法(见 数据绑定 部分)。


13
2017-09-18 13:09



Xamarin.Forms  BindableObject没有 DataContext 属性,但是 BindingContext - Stephane Delcroix
谢谢(你的)信息! - James Wright