Мой вызов API возвращает данные из базы данных, как и ожидалось, хотя элементы в моей ObservableCollection не отображаются в приложении при отладке.
Это моя разметка XAML:
Код: Выделить всё
Код: Выделить всё
using SaleBeaconMobile.Models;
using SaleBeaconMobile.Services;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using Microsoft.Maui.Controls.Xaml;
namespace SaleBeaconMobile.ViewModels
{
public class MainPageCusViewModel : BaseViewModel
{
private readonly DataService _dataService = new DataService();
private const int PageSize = 10;
private int _currentPage = 0; // Track the current page for loading more data
public ObservableCollection Items { get; }
private bool _isRefreshing;
public bool IsRefreshing
{
get => _isRefreshing;
set
{
_isRefreshing = value;
OnPropertyChanged();
}
}
public ICommand LoadMoreItemsCommand { get; }
public ICommand RefreshCommand => new Command(async () => await DownloadDataAsync());
public MainPageCusViewModel()
{
Items = new ObservableCollection();
// Assign the command to LoadMoreItemsCommand
LoadMoreItemsCommand = new Command(async () => await LoadMoreItemsAsync());
// Trigger initial data load
Task.Run(async () => await DownloadDataAsync());
}
// Method to load initial data
public async Task DownloadDataAsync()
{
try
{
IsRefreshing = true;
// Fetch initial data (page 0 or starting page)
var items = await _dataService.GetItemsAsync(0, PageSize);
// Clear existing items and add new items
Items.Clear();
foreach (var item in items)
{
Items.Add(item);
}
// Update the current page
_currentPage = 1; // Assuming pages start from 0
}
catch (Exception ex)
{
// Handle exceptions as needed
}
finally
{
IsRefreshing = false;
}
}
// Method to load more items for pagination or infinite scrolling
public async Task LoadMoreItemsAsync()
{
try
{
if (IsRefreshing)
return; // Prevent re-entry
IsRefreshing = true;
// Fetch more data based on the current page
var items = await _dataService.GetItemsAsync(_currentPage, PageSize);
// Add new items to the existing collection
foreach (var item in items)
{
Items.Add(item);
}
// Increment the current page for the next load
_currentPage++;
}
catch (Exception ex)
{
// Handle exceptions as needed
}
finally
{
IsRefreshing = false;
}
}
}
}
Спасибо за любые подсказки и помощь! Приветствую, Джо
Подробнее здесь: https://stackoverflow.com/questions/786 ... n-api-call