Binding Issue to ListView

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Binding Issue to ListView



We are a newbie for Xamarin. We are having an issue to binding the data from web service into ListView. We debugged and we can see the data coming in from the web service but it never populated. Any ideas? It's gotta be a small thing that we miss. We managed to display with other view using a single entry from the web service in other code that we have BUT not this IEnumerable<> or List<>



Here's the code:



View - RoundsPage.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:AthlosifyMobile.ViewModels"
x:Class="AthlosifyMobile.Views.RoundsPage">

<ContentPage.BindingContext>
<viewModels:RoundsViewModel />
</ContentPage.BindingContext>

<StackLayout>

<Entry Text="Binding AccessToken" />
<Button Command="Binding GetRoundsCommand" Text="Get all rounds" />
<Label Text="Rounds: "></Label>
<ListView ItemsSource="Binding Rounds" HasUnevenRows="true" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="Round 1:"></Label>
<Label Text="Binding Name"></Label>
<Label Text="Binding DailyHandicap"></Label>
<Label Text="Binding PlayedUTC"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>`



ViewModel - RoundsViewModel.cs :


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using AthlosifyMobile.Annotations;
using Xamarin.Forms;
using AthlosifyMobile.Services;
using AthlosifyMobile.Models;

namespace AthlosifyMobile.ViewModels

public class RoundsViewModel : INotifyPropertyChanged

ApiServices _apiServices = new ApiServices();
public event PropertyChangedEventHandler PropertyChanged;
private IEnumerable<Round> _rounds;
public string AccessToken get; set;
public IEnumerable<Round> Rounds

get

return _rounds;

set

_rounds = value;
OnPropertyChanged();



public ICommand GetRoundsCommand

get

return new Command(async() =>

Rounds = await _apiServices.GetRoundsAsync(AccessToken);
);



[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));





Model - Course.cs


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

namespace AthlosifyMobile.Models

public class Round : EntityBase

public Guid RoundID get; set;
public Guid UserID get; set;
public Guid RoundCategoryID get; set;
public Guid CourseID get; set;
public string Name get; set;
public string Notes get; set;
public int DailyHandicap get; set;
public DateTime PlayedUTC get; set;
public RoundCategory RoundCategory get; set;
public Course Course get; set;

public ICollection<RoundHole> RoundHoles get; set;


public abstract class EntityBase

public DateTime CreatedUTC get; set;

public DateTime LastModifiedUTC get; set;




Services - apiservices.cs:


using AthlosifyMobile.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace AthlosifyMobile.Services


public async Task<IEnumerable<Round>> GetRoundsAsync(string accessToken)

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var json = await client.GetStringAsync("http://localhost:5609/api/Rounds");
var list = JsonConvert.DeserializeObject<IEnumerable<Round>>(json);

return list;


}





first, are you certain that your GetRoundsAsync() is returning data? 2nd, your setter for Rounds is replacing the existing instance with a new instance, which may be breaking your binding. Try explicitly setting the ListView's ItemsSource in code after you have retrieved the new data.
– Jason
28 mins ago





When we debugged this code, we can see data returning in GetRoundsAsync(). In regards #2, how do we do that?
– dcpartners
24 mins ago






is the data getting deserialized correctly? For the 2nd, assign a name to your ListView in XAML, then just assign myList.ItemsSource after the data is loaded.
– Jason
17 mins ago





Data has been deseriliased correctly. Not sure how to assign this myList.ItemsSource = Rounds; ?!?! ie linking to RoundViewModel where the iEnumerable<Round> exist
– dcpartners
3 mins ago










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Hystrix command on request collapser fallback