hello how are you , please i have problem with load more list view how to get total items from web service before checkCanLoadMoreItems . because the web service use await i get total items always 0 before check if (Events.Count >= totalItems) 10 days no solution i use wait function the main thread in stopped . this is code
public class EventViewModel : INotifyPropertyChanged
{
#region Evenets
private void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Attributes
private ApiService apiService;
private DialogService dialogService;
private NavigationService navigationService;
private int totalItems=0 ;
#endregion
#region Properties
public ObservableCollection Events { get; set; }
public Command<object> LoadMoreItemsCommand { get; set; }
#endregion
#region Constructors
public EventViewModel()
{
apiService = new ApiService();
dialogService = new DialogService();
navigationService = new NavigationService();
Events = new ObservableCollection();
LoadMoreItemsCommand = new Command<object>(LoadMoreItems, CanLoadMoreItems);
}
private bool CanLoadMoreItems(object obj)
{
totalItems = (int) EventCount().Result;
dialogService.ShowMessage("Number of items from web service", totalItems.ToString());
//always get 0 because await it is not finish
if (Events.Count >= totalItems)
return false;
return true;
}
private async void LoadMoreItems(object obj)
{
var listview = obj as Syncfusion.ListView.XForms.SfListView;
listview.IsBusy = true;
var index = Events.Count;
var count = index + 3 >= totalItems ? totalItems - index : 3;
await LoadEvents(index, count);
listview.IsBusy = false;
}
// function for get totalItems count from web service
private async Task EventCount()
{
var urlBase = Application.Current.Resources["URLBase"].ToString();
var response = await apiService.GetCount(
urlBase,
"/api",
"/Eventcount",
Settings.TokenType,
Settings.Token);
if (!response.IsSuccess)
{
await dialogService.ShowMessage("Error", response.Message);
return null;
}
return response;
}
// function for get only 3 items from web service
private async Task LoadEvents(int index, int count)
{
var urlBase = Application.Current.Resources["URLBase"].ToString();
// var user = dataService.First(false);
var user = MainViewModel.GetInstance().CurrentUser;
var response = await apiService.Get(
urlBase,
"/api",
"/Events",
index,
count,
Settings.TokenType,
Settings.Token);
if (!response.IsSuccess)
{
await dialogService.ShowMessage("Error", response.Message);
return;
}
ReloadEvents((List)response.Result);
}
private void ReloadEvents(List tournaments)
{
foreach (var tournament in tournaments)
{
Events.Add(new EventItemViewModel
{
EventId = tournament.EventId,
UserId = tournament.UserId,
Name = tournament.Name,
Date = tournament.Date,
Place = tournament.Place,
Description = tournament.Description,
Latitude = tournament.Latitude,
Longitude = tournament.Longitude,
User = tournament.User,
EventUsers = tournament.EventUsers,
});
}
}