xamarin.forms XAML的财产绑定 [英] xamarin.forms binding from xaml to property

查看:306
本文介绍了xamarin.forms XAML的财产绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手总在XAML绑定和我真的没有有时会它



我有这在我的XAML:



<预类=郎咸平的XML prettyprint-覆盖> < ActivityIndi​​cator IsRunning ={结合IsLoading}ISVISIBLE ={结合IsLoading}/>



绑定IsLoading。我在哪里声明/设置该属性。



我的.cs如下:?!



<预类=郎-cs prettyprint-覆盖> ....
公共BOOL IsLoading;

公共CardsListXaml()
{
的InitializeComponent();
IsLoading = TRUE;
....


解决方案

绑定通常从的 BindingContext属性解析(在其他实现,这个属性被称为的DataContext )。这是默认情况下(至少在XAML的其他实现),因此,您的看法是无法找到指定的属性。



在你的情况,你必须在的BindingContext 属性设置为这个

 公共CardsListXaml()
{
的InitializeComponent();
的BindingContext =这一点;
IsLoading = TRUE;
}



不过,仅此是不够的。您当前的解决方案不落实的机制来通知的任何属性更改视图,因此您的视图将不得不实施 INotifyPropertyChanged的。相反,我建议你实现href=\"http://msdn.microsoft.com/en-us/library/ff798384.aspx\">模型 - 视图 - 视图模型的模式,这不仅符合精美的

 公共类CardsListViewModel:INotifyPropertyChanged的
{
私人布尔isLoading;
公共BOOL IsLoading
{
得到
{
返回this.isLoading;
}


{
this.isLoading =价值;
RaisePropertyChanged(IsLoading);
}
}

公共CardsListViewModel()
{
IsLoading = TRUE;
}

//视图将注册时,DataContext设置
公共事件PropertyChangedEventHandler的PropertyChanged本次活动;

公共无效RaisePropertyChanged(字符串作为propName)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(作为propName)) ;
}
}
}



然后在你的代码 - 背后的构造函数:

 公共CardsListView()
{
的InitializeComponent();
的BindingContext =新CardsListViewModel();
}



只是为了澄清,的DataContext 瀑布下的可视化树,因此 ActivityIndi​​cator 控件将能够读取在绑定指定的属性。



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


I am a total newbie with bindings in xaml and I really don't get it sometimes.

I have this in my xaml:

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

The binding "IsLoading". Where do I declare/set this property?!

My .cs looks like this:

....
    public bool IsLoading;

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

解决方案

Bindings are typically resolved from the BindingContext property (in other implementations, this property is called DataContext). This is null by default (at least in other implementations of XAML), thus your view is unable to find the specified properties.

In your case, you must set the BindingContext property to this:

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

However, this alone will not suffice. Your current solution does not implement a mechanism to notify the view of any property changes, so your view would have to implement INotifyPropertyChanged. Instead, I suggest you implement the Model-View-ViewModel pattern, which not only fits beautifully with data binding, but will result in a more maintainable and testable code base:

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));
        }
    }
} 

And then in your code-behind's constructor:

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

Just to clarify, DataContext cascades down the visual tree, thus the ActivityIndicator control will be able to read to properties specified in the bindings.

Edit: Xamarin.Forms (and Silverlight/WPF etc... sorry, it's been a while!) also provides a SetBinding method (see the Data Binding section).

这篇关于xamarin.forms XAML的财产绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆