Jaycee Evangelista
Jaycee Evangelista

Reputation: 1127

Connecting Xamarin.Forms to Web Services

Good Day Everyone. I want all the records created in ASP.NET Web Application to be shown in my Mobile App Xamarin.Forms. What's happening to my program is that I was able to create records in my Web Application and save it, but I wasn't able to make it appear in my Xamarin.Forms Mobile app. I have created a MainViewModel that will get the records from the Web Application which I have binded to my MainPage. These are my codes:

MainPageMain.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:local="clr-namespace:XamarinDemoApp"
         x:Class="XamarinDemoApp.MainPageMain"
         xmlns:ViewModels="clr-namespace:XamarinDemoApp.ViewModels;assembly=XamarinDemoApp"
         BackgroundColor="Teal"
         Title=" Title Bar">



 <ContentPage.BindingContext>
     <ViewModels:MainViewModel/>
 </ContentPage.BindingContext>


 <StackLayout Orientation="Vertical">

   <ListView ItemsSource="{Binding EmployeesList}" HasUnevenRows="True">
     <ListView.ItemTemplate>
       <DataTemplate>
          <ViewCell>
            <StackLayout Orientation="Horizontal">
              <Label Text="{Binding Name}" FontSize="24"/>
              <Label Text="{Binding Department}" FontSize="24"/>
            </StackLayout>
          </ViewCell>
       </DataTemplate>
     </ListView.ItemTemplate>
   </ListView>

   <Label Text="This is the MainPage"/>

 </StackLayout>

MainViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using XamarinDemoApp.Models;
using XamarinDemoApp.Services;

namespace XamarinDemoApp.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private List<Employee> _employeesList;

        public List<Employee> EmployeesList
        {
            get { return _employeesList; }
            set
            {
                _employeesList = value;
                OnPropertyChanged();
            }
        }

        public MainViewModel()
        {
           InitializeDataAsync();
        }

        private async Task InitializeDataAsync()
        {
            var employeesServices = new EmployeesServices();
            EmployeesList = await employeesServices.GetEmployeesAsync();
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Upvotes: 0

Views: 246

Answers (1)

GiampaoloGabba
GiampaoloGabba

Reputation: 1458

You should use ObservableCollections, something like this:

    private ObservableCollection<Employee> _employeesList;
    public ObservableCollection<Employee> EmployeesList
    {
        get { return _employeesList; }
        set { Set(() => EmployeesList, ref _employeesList, value); }
    }

then

    private async Task InitializeDataAsync()
    {
        var employeesServices = new EmployeesServices();
        var employees = await employeesServices.GetEmployeesAsync();
        EmployeesList = new ObservableCollection<Employee>(employees);
    }

Upvotes: 0

Related Questions