This recipe introduces the concept of data binding some object instance properties to view controls in XAML.
Start by creating a Visual Studio Blank App (Xamarin.Forms Portable) solution. In the top menu, click File | New | Project… and give it the name
XamFormsXamlBinding
.In the
XamFormsXamlBinding
PCL library, right-click and choose Add | Class…; give it the namePerson.cs
and click Add.Copy the following code to add two simple properties in
Person.cs
:public class Person { public string FirstName { get; set; } public string LastName { get; set; } }
Right-click the
XamFormsCodeBinding
PCL and Add | New Item…; choose Forms Xaml Page, name itMainPage.xaml
, and click Add. In theContent
tag, add the following code that adds twoLabel
controls to bind toPerson
properties directly in XAML:<StackLayout HorizontalOptions="Center" VerticalOptions="Center"> <Label Text="{Binding FirstName}" /> <Label Text="{Binding LastName}" /> <...