Example #1
0
        public ListViewModel()
        {
            this.Source = new ListViewLoadOnDemandCollection((cancelationToken) =>
            {
                List <string> result = new List <string>();

                try
                {
                    //simulates connection latency
                    Task.Delay(4000, cancelationToken).Wait();

                    this.lodTriggerCount++;
                    foreach (string item in Enum.GetNames(typeof(DayOfWeek)))
                    {
                        result.Add(string.Format("LOD: {0} - {1}", lodTriggerCount, item));
                    }
                    return(result);
                }
                catch (Exception ex)
                {
                    //exception is thrown when the task is canceled. Returning null does not affect the ItemsSource.
                    return(null);
                }
            });

            for (int i = 0; i < 14; i++)
            {
                Source.Add(string.Format("Item {0}", i));
            }
        }
        public CharactersViewModel()
        {
            Title = "Characters";

            Characters = new ListViewLoadOnDemandCollection((token) =>
            {
                try
                {
                    // Since the caller run this delegate in a separate task, we can use .Result here safely and it wont block UI thread
                    var apiResult = ApiService.GetCharactersAsync(CurrentCharactersCount, 50).Result;

                    if (apiResult != null)
                    {
                        // We need to marshal to UI thread to update these properties
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            CurrentCharactersCount = apiResult.Offset + apiResult.NumberOfPageResults;
                            TotalCharactersCount   = apiResult.NumberOfTotalResults;
                        });

                        return(apiResult.Results);
                    }
                }
                catch (Exception exception)
                {
                    Debug.WriteLine($"FetchMoreCharacters Exception: {exception}");
                }

                return(null);
            });
        }