CollectionView in .NET MAUI is not displaying items returned from an API call

I have an existing app that I need to upgrade to MAUI, having a slight issue on the display end. I have migrated up to .NET MAUI just recently and I know that I am close to getting these suckers displaying, though I am having a slight issue, I just don’t know where exactly.

My API call returns the data as expected from the database, though the items in my ObservableCollection are not being displayed in the app when debugging.

This is my XAML markup:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SaleBeaconMobile.Views.MainPageCus"
             Title="Sale Alerts"
             xmlns:ViewModels="clr-namespace:SaleBeaconMobile.ViewModels">

    <ContentPage.BindingContext>
        <ViewModels:MainPageCusViewModel />
    </ContentPage.BindingContext>

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Debug" Clicked="ToolbarItem_Clicked_3" Order="Secondary" />
        <ToolbarItem Text="Settings" Clicked="ToolbarItem_Clicked"  Order="Secondary"/>
        <ToolbarItem Text="Support" Clicked="ToolbarItem_Clicked_1"  Order="Secondary"/>
        <ToolbarItem Text="Log Out" Clicked="ToolbarItem_Clicked_2"  Order="Secondary"/>
    </ContentPage.ToolbarItems>

    <ContentPage.Content>
        <RefreshView>
            <CollectionView x:Name="CVSaleAlerts"
                    ItemsSource="{Binding Items}"
                    ItemsLayout="VerticalList"
                    RemainingItemsThreshold="4"
                    RemainingItemsThresholdReachedCommand="{Binding LoadMoreItemsCommand}"
                    HeightRequest="300">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid Padding="10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="280" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="350" />
                            </Grid.ColumnDefinitions>
                            <Image Source="{Binding bg}"
                           Grid.Row="0"
                           Grid.Column="0"
                           Aspect="AspectFill"
                           WidthRequest="350"
                           HeightRequest="280"
                           MinimumHeightRequest="280"
                           VerticalOptions="FillAndExpand" />
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </RefreshView>
    </ContentPage.Content>
</ContentPage>

This is my view model:

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<Sale> 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<Sale>();

            // 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;
            }
        }
    }
}

I know that I am pretty close as the data is being retrieved properly, the idea is to bring back 10 records at a time from my API call, all of that works in postman as well in my older app. I am just unsure on the display end currently, not sure if it is an attribute I am missing or what. I was using a ListView with a collection prior to this and that all works but I think that I should be upgrading it all to .MAUI with the new attributes and features that comes along with that. I am not receiving any errors currently.

Thanks for any pointers and help! Cheers, Joe 🙂

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật