概念
數(shù)據(jù)綁定連接兩個(gè)對(duì)象臂外,稱為源和目標(biāo)伴澄。源對(duì)象提供數(shù)據(jù)。目標(biāo)對(duì)象將消耗(并經(jīng)常顯示)來(lái)自源對(duì)象的數(shù)據(jù)账劲。例如戳护,Label(目標(biāo)對(duì)象)通常將其Text屬性綁定到源對(duì)象中的公共字符串屬性。下圖說(shuō)明了綁定關(guān)系:
數(shù)據(jù)綁定的主要好處是您不再需要擔(dān)心在視圖和數(shù)據(jù)源之間同步數(shù)據(jù)瀑焦。源對(duì)象中的更改被綁定框架自動(dòng)推送到目標(biāo)對(duì)象的幕后腌且,目標(biāo)對(duì)象中的更改可以選擇性地推回到源對(duì)象
建立數(shù)據(jù)綁定是一個(gè)兩步過(guò)程
目標(biāo)對(duì)象的BindingContext屬性必須設(shè)置為源
目標(biāo)和來(lái)源之間必須建立約束。在XAML中榛瓮,這是通過(guò)使用綁定標(biāo)記擴(kuò)展來(lái)實(shí)現(xiàn)的铺董。在C#中,這是通過(guò)SetBinding方法實(shí)現(xiàn)的
有關(guān)數(shù)據(jù)綁定的更多信息榆芦,請(qǐng)參閱https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/
綁定方式
1.XAML
2.C#
XAML
前端Binding
<StackLayout Padding="0,20,0,0">
<Label Text="Forename:" />
<Entry Text="{Binding Forename, Mode=TwoWay}" />
<Label Text="Surname:" />
<Entry Text="{Binding Surname, Mode=TwoWay}" />
<StackLayout Padding="0,20,0,0" Orientation="Horizontal">
<Label Text="Your forename is:" />
<Label Text="{Binding Forename}" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Your surname is:" />
<Label Text="{Binding Surname}" />
</StackLayout>
</StackLayout>
后臺(tái)BindingContext賦值
BindingContext = new DetailsViewModel();
DetailsViewModel類
public class DetailsViewModel : INotifyPropertyChanged
{
string forename, surname;
public string Forename
{
get
{
return forename;
}
set
{
if (forename != value)
{
forename = value;
OnPropertyChanged("Forename");
}
}
}
public string Surname
{
get
{
return surname;
}
set
{
if (surname != value)
{
surname = value;
OnPropertyChanged("Surname");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
示例代碼
https://github.com/zLulus/NotePractice/tree/dev3/Xamarin.Forms/XamarinDemo/XamarinDemo/XamarinDemo/Bindings 的BindingFirstName2
使用內(nèi)置模板TextCell進(jìn)行數(shù)據(jù)綁定(C#)
進(jìn)行數(shù)據(jù)綁定
//Model
Employee employeeToDisplay = new Employee();
//輸入框
var firstNameEntry = new Entry()
{
HorizontalOptions = LayoutOptions.FillAndExpand
};
//綁定 下面兩個(gè)綁定柄粹,任選其一即可
this.BindingContext = employeeToDisplay;
firstNameEntry.SetBinding(Entry.TextProperty, "FirstName");
Employee類
實(shí)現(xiàn)接口INotifyPropertyChanged喘鸟,F(xiàn)irstName在set時(shí),觸發(fā)OnPropertyChanged方法
public class Employee : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
if (value.Equals(_firstName, StringComparison.Ordinal))
{
// Nothing to do - the value hasn't changed;
return;
}
_firstName = value;
OnPropertyChanged();
}
}
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
查看綁定結(jié)果
//查詢按鈕
Button getValueButton = new Button();
getValueButton.Text = "查看結(jié)果";
getValueButton.Clicked += (async (sender, e) =>
{
await DisplayAlert("綁定結(jié)果",$"當(dāng)前Entry的Text是{firstNameEntry.Text},后臺(tái)實(shí)體的FirstName是{employeeToDisplay.FirstName}","確定");
});
示例代碼
https://github.com/zLulus/NotePractice/tree/dev3/Xamarin.Forms/XamarinDemo/XamarinDemo/XamarinDemo/Bindings 的BindingFirstName