Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

MVVM DataBinding ListView is Empty (please help)

$
0
0

I am using Xamarin Forms Mvvm DataBinding with web service but ListView is empty . this is my all code . I can open asp.net web service from emulator

and real device from browser.

===================MainPage.xaml===============================================

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage.BindingContext>

  <viewModels:MainViewModels/>

</ContentPage.BindingContext>

<ListView x:Name="MainListView" HasUnevenRows="True" Margin="10" ItemsSource="{Binding employeesList}" >

    <ListView.ItemTemplate>

        <DataTemplate>

            <ViewCell>

                <StackLayout Padding="6,12">

                    <Label Text="{Binding Name}"/>

                    <Label Text="{Binding Department}"/>


                </StackLayout>

            </ViewCell>

        </DataTemplate>

    </ListView.ItemTemplate>

</ListView>

======================MainViewModels==================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using App4.Models;
using App4.Services;

namespace App4.ViewModels
{
public class MainViewModels:INotifyPropertyChanged
{
private List _employeesList;

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


    public MainViewModels()
    {
       InitilizeDataAsync();
    }


    public async Task InitilizeDataAsync()
    {
        var employeesService = new EmployeesService();
        employeesList = await employeesService.GetEmployeesAsync();
    }



    public event PropertyChangedEventHandler PropertyChanged;

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

}

===================EmployeesService==========================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using App4.Models;

namespace App4.Services
{
class EmployeesService
{

    public async Task<List<Employee>> GetEmployeesAsync()
    {

        RestClient<Employee> restClient =new RestClient<Employee>();

        var employeesList = await restClient.GetAsync();

        return  employeesList;
    }

}

}

=======================RestClient============================

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace App4
{
public class RestClient
{
public string WebServiceUrl = "link/api/Employees";

    public async Task<List<T>> GetAsync()
    {
        var httpClient = new HttpClient();


        var json = await httpClient.GetStringAsync(WebServiceUrl);

        var emploemployeesList = JsonConvert.DeserializeObject<List<T>>(json);

        return emploemployeesList;

    }


}

}

==========================Employee================================

using System;
using System.Collections.Generic;
using System.Text;

namespace App4.Models
{
public class Employee
{

    public int  Id { get; set; }

    public string Name { get; set; }

    public string Department { get; set; }

}

}

====================================================================

When write code behind (Rest Client ) Class the List View is fill and all thing is right. any solution please

===================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using App4.Models;
using App4.Services;
using App4.ViewModels;
using Newtonsoft.Json;
using Xamarin.Forms;

namespace App4
{
public partial class MainPage : ContentPage
{

    public MainPage()
    {
           InitializeComponent();

        LoadEmployees();

         }


    private async Task    LoadEmployees()

    {
         string WebServiceUrl = "link/api/Employees/";


             var httpClient = new HttpClient();

            var json = await httpClient.GetStringAsync(WebServiceUrl);

        var taskExam = JsonConvert.DeserializeObject<List<Employee>>(json);

        MainListView.ItemsSource = taskExam;



    }
}

}


Viewing all articles
Browse latest Browse all 204402

Trending Articles