Hello. Please tell me how to correctly implement the repeated operation of the command to receive data if the first attempt was not successful and the SfListView does not have previously loaded data.
Problem example:
Opening the MainPage
The command to load data is run, but an error occurs(for example no internet connection)
The SfListView has no data because it was not loaded in the previous step
Perhaps there is a parameter responsible for re-requesting data until the first data is loaded?
Unfortunately, the automatic start of the command occurs only if the screen orientation changes (vertical-horizontal)
Perhaps there is a property responsible for re-running the command until the first data is successfully received?
<sflv:SfListView x:Name="listView"
ItemsSource="{Binding Items}"
ItemSize="{OnPlatform Android={OnIdiom Phone=170, Tablet=280}}"
AutoFitMode="Height"
LoadMoreOption="Auto"
LoadMoreCommand="{Binding LoadSeriesCommand}">
public class SeriesViewModel
{
public ICommand LoadSeriesCommand => new Command(async () => await LoadSeriesAsync());
async Task LoadSeriesAsync()
{
if (IsBusy)
return;
try
{
IsBusy = true;
var series = await _seriesService.GetSeries();
foreach (var item in series)
Items.Add(item.Map());
}
catch (Exception ex)
{
Debug.WriteLine($"Unable to get series {ex.Message}");
await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
}
finally
{
IsBusy = false;
}
}
Hi Oct,
We would like to let
you know that, you can load the data at runtime in LoadMoreCommand , by setting
the CanExecute method based on the required criteria, you can check the condition
where you want to load more the items or not on the
CanExecute method in LoadMoreCommand , Please
refer the below code snippet and documentation for more reference.
Code snippet:
In ViewModel.cs
LoadMoreItemsCommand = new Command<object>(LoadMoreItems, CanLoadMoreItems);
private bool CanLoadMoreItems(object obj) { if (Products.Count >= totalItems) return false; return true; }
|
Regards,
Suthi Yuvaraj
Thanks. Unfortunately, in this variant, the method call also occurs 1 time. If the first method call was not successful, then the second time the method is not run either.
step1. Open page
step2. SfListView init command
step3. execute CanLoadMoreItems(return true)
step4. execute LoadSeriesAsync(error has occurred) Items.Count=0
step5. I want the ' LoadSeriesCommand ' command to be called again.
Of course, I can execute the command again in the finaly block in case of an error, but this option seems bad to me.
finally
{
IsBusy = false;
if(Items.Count = 0){
LoadSeriesCommand.Execute();
}
}
public ICommand LoadSeriesCommand => new Command(LoadSeriesAsync, CanLoadMoreItems);
private bool CanLoadMoreItems(object obj) {
if (Items.Count >= 0)
return true;
return false;
}
async void LoadSeriesAsync(object obj)
{
if (IsBusy)
return;
try
{
IsBusy = true;
var series = await _seriesService.GetSeries();
//Add mapper
if (Items.Count != 0)
{
Items.Clear();
return;
}
foreach (var item in series)
Items.Add(item.Map());
}
catch (Exception ex)
{
Debug.WriteLine($"Unable to get series {ex.Message}");
await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
}
finally
{
IsBusy = false;
}
}
Oct,
We would like to inform you that there is no need to manually call the LoadMoreCommand since it is designed to execute automatically until the CanExecute method returns false.
We have tested the code snippet you provided and noticed that the items collection is being cleared inside the LoadMoreCommand, which causes each item in the collection to be cleared. We have modified the sample as per your requirements.
Please take a look at the updated sample and let us know if you have any concerns.
Suthi, thanks a lot