Hi,
I am developing sample application using Xamarin.Forms. Here we need to bind the JSON data(List) from WCF Service to ListView .
But i could not able to bind the data in Listview.
Please find the code
Model Class
public class **ModelObject**
{
public Object HD_Code { get; set; }
public String Description { get; set; }
}
ViewModel Class
public class **SearchRequestViewModel**:INotifyPropertyChanged
{
**public List<ModelObject> myDeserializedObjList = new List<ModelObject>();**
public SearchRequestViewModel()
{
try
{
LoadSite();
}
catch(Exception ex)
{
}
}
public async void LoadSite()
{
**//calling WCF Service to get the value for list **
List<ModelObject> SampleList = await Service.GetUsersite(Parameters);
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
// **value successfully retrieved from WCF Service and assigned to the myDeserializedObjList but UI didn’t update the listview**
myDeserializedObjList= SampleList;
});
}
public List<ModelObject> **ListViewSource**
{
get { return myDeserializedObjList; }
set
{
if (myDeserializedObjList!= null)
{
myDeserializedObjList = value;
OnPropertyChanged("ListViewSource");
}
}
}
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
View in 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:Sample.ViewModels;assembly= Sample "
x:Class=" Sample.Views.SearchreqFilter" Title="Filter">
<ContentPage.BindingContext>
<local:**SearchRequestViewModel**/>
</ContentPage.BindingContext>
<ListView x:Name="Site_listView" ItemsSource="{Binding **ListViewSource**}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Vertical"
HorizontalOptions="StartAndExpand">
<Label Text="{Binding **Description**}"
HorizontalOptions="FillAndExpand" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ ContentPage>
Kindly suggest me on this.