Search This Blog

Thursday 12 November 2020

Xamarin Forms Compile Binding Demo

Compile Time Binding in Content Page & DataTemplate

Views(EmployeePage.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"
             xmlns:viewModels="clr-namespace:CompileTimeBinding.ViewModels"
             xmlns:models ="clr-namespace:CompileTimeBinding.Models"
             x:DataType="viewModels:EmployeePageViewModel"
             x:Class="CompileTimeBinding.Views.EmployeePage">
    <ContentPage.BindingContext>
        <viewModels:EmployeePageViewModel />
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout Margin="10" >
            <ListView SeparatorVisibility="None" HasUnevenRows="True" ItemsSource="{Binding Employees}" >
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="models:Employee">
                        <ViewCell>
                            <StackLayout Margin="10">
                                <Label >
                                    <Label.FormattedText>
                                        <FormattedString>
                                            <Span Text="{Binding FirstName}" />
                                            <Span Text=" " />
                                            <Span Text="{Binding LastName}" />
                                        </FormattedString>
                                    </Label.FormattedText>
                                </Label>
                                <Label Text="{Binding Email}" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>


EmployeePage.xaml.cs

using CompileTimeBinding.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
 
namespace CompileTimeBinding.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class EmployeePage : ContentPage
    {
        public EmployeePage()
        {
            InitializeComponent();
        }
    }
}


ViewModels (EmployeePageViewModel.cs)

using CompileTimeBinding.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
 
namespace CompileTimeBinding.ViewModels
{
    public class EmployeePageViewModel
    {
        #region Properties
 
        public ObservableCollection<Employee> Employees { get; set; }
 
        #endregion
 
        #region Constructor
        public EmployeePageViewModel()
        {
            Employees = new ObservableCollection<Employee>();
            Employee obj = new Employee();
            obj.FirstName = "Pragnesh";
            obj.LastName = "Mistry";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
            obj = new Employee();
            obj.FirstName = "Vishal";
            obj.LastName = "Makvana";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
            obj = new Employee();
            obj.FirstName = "Nirav";
            obj.LastName = "Tailor";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
 
            obj = new Employee();
            obj.FirstName = "Viral";
            obj.LastName = "Mistry";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
            obj = new Employee();
            obj.FirstName = "Bhavik";
            obj.LastName = "Mistry";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
            obj = new Employee();
            obj.FirstName = "Jignesh";
            obj.LastName = "Tailor";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
            obj = new Employee();
            obj.FirstName = "Sachin";
            obj.LastName = "Shinde";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
 
            obj = new Employee();
            obj.FirstName = "Viral";
            obj.LastName = "Patel";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
 
 
            obj = new Employee();
            obj.FirstName = "Mayur";
            obj.LastName = "Patel";
            obj.Email = "abc@gmail.com";
            Employees.Add(obj);
        }
        #endregion
    }
}
 

Models (Employee.cs)

using System;
using System.Collections.Generic;
using System.Text;
 
namespace CompileTimeBinding.Models
{
    public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
    }
}
 

App.xaml.cs

using CompileTimeBinding.Views;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
 
namespace CompileTimeBinding
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
 
            MainPage = new EmployeePage();
        }
 
        protected override void OnStart()
        {
        }
 
        protected override void OnSleep()
        {
        }
 
        protected override void OnResume()
        {
        }
    }
}
 




1 comment:

  1. thanks for sharinf wonderful information. guys you can find some more information related to React Native vs Xamarin

    ReplyDelete

Popular Posts