Hello everyone,
I am having a really hard time getting the groups to display on my listview. I used the guide posted by @JamesMontemagno
[
motzcod.es/post/94643411707/enhancing-xamarinforms-listview-with-grouping
Here is what I have:
ViewModel:
public async Task LoadEventLists()
{
if (IsBusy)
return;
IsBusy = true;
var error = false;
try
{
var feedString = string.Empty;
using (var httpClient = new HttpClient())
{
var feed = "http://api.gcm.li/events/index.xml";
feedString = await httpClient.GetStringAsync(feed);
}
var newItems = await ParseEvents(feedString);
if (EventsItems.Count == 0) {
foreach (var item in newItems) {
EventsItems.Add(item);
}
} else if (EventsItems.Count > 0) {
var oldItems = EventsItems.ToList();
List<CalendarModel> newList = newItems.Except(oldItems, new CalendarComparer()).ToList();
foreach (var item in newList)
EventsItems.Add(item);
}
var sorted = from events in EventsItems
orderby events.Date
group events by events.EventSort into eventGroup
select new Grouping<string, CalendarModel>(eventGroup.Key, eventGroup);
EventsGrouped = new ObservableCollection<Grouping<string, CalendarModel>>(sorted);
}
catch
{
error = true;
}
if (error)
{
var page = new ContentPage();
await page.DisplayAlert("Error", "Unable to load upcoming events.", "OK");
}
IsBusy =
false;
}
private async Task<List<CalendarModel>> ParseEvents(String rss)
{
return await Task.Run (() => {
var xdoc = XDocument.Parse (rss);
var id = 0;
return (
from item in xdoc.Descendants ("event")
select new CalendarModel {
Date = (string)item.Element ("date"),
EventName = (string)item.Element ("event_name"),
StartTime = (string)item.Element("time_string"),
Location = (string)item.Element("location"),
Description = Regex.Replace((string)item.Element("event_description"), @"<[^>]*>", String.Empty),
LeaderName = (string)item.Element("leader_name"),
Id = id++,
}).ToList();
});
}
Model:
public class CalendarModel : INotifyPropertyChanged
{
public int Id { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public string Date { get; set; }
public string EventName { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Location { get; set; }
public string EventSort
{
get
{
if (string.IsNullOrWhiteSpace(Date) || Date.Length == 0)
return "?";
return Date.ToString().ToUpper();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
My View:
public class UpcomingEventsView : BaseView
{
private UpcomingEventsViewModel _viewModel
{
get { return BindingContext as UpcomingEventsViewModel; }
}
public UpcomingEventsView ()
{
BindingContext = new UpcomingEventsViewModel();
ToolbarItems.Add(new ToolbarItem("search", "ic_today.png", async () =>
{
await Navigation.PopModalAsync();
}));
var memberList = new ListView ();
memberList.IsPullToRefreshEnabled = true;
memberList.ItemsSource = _viewModel.EventsGrouped;
memberList.IsGroupingEnabled = true;
memberList.GroupDisplayBinding = new Binding("Key");
memberList.GroupShortNameBinding = new Binding("Key");
if(Device.OS != TargetPlatform.WinPhone)
memberList.GroupHeaderTemplate = new DataTemplate(typeof(HeaderCell));
memberList.HasUnevenRows = true;
var cell = new DataTemplate(typeof(TextCell));
cell.SetBinding(TextCell.TextProperty, "EventName");
cell.SetBinding(TextCell.DetailProperty, "StartTime");
memberList.ItemTemplate = cell;
//Set Bindings
memberList.SetBinding<UpcomingEventsViewModel>(ListView.IsRefreshingProperty, vm => vm.IsBusy, BindingMode.OneWay);
memberList.SetBinding<UpcomingEventsViewModel>(ListView.RefreshCommandProperty, vm => vm.LoadEventsCommand);
Content = memberList;
}
protected override async void OnAppearing()
{
base.OnAppearing();
_viewModel.LoadEventsCommand.Execute(null);
}
}
My issue is that nothing shows on the listview. The EventsItems add to the listview just fine, but the grouping will not. I'm not sure what I am doing wrong, but I could really use some help right about now. Thanks.