.NET Maui中CollectionView绑定ObservableCollection不显示数据
我正在开发.NET Maui应用,在ScrollView中放置了一个CollectionView,希望将其ItemsSource绑定到ViewModel中的ObservableCollection
相关代码
XAML代码
<RefreshView x:DataType="vm:TodayViewModel" Command="{Binding LoadStandInsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}"> <CollectionView ItemsSource="{Binding StandIns}" SelectionMode="None"> <CollectionView.ItemTemplate> <DataTemplate> <StackLayout Padding="10" x:DataType="model:StandIn" BackgroundColor="Red"> <Frame HeightRequest="50" > <StackLayout> <Label Style="{StaticResource BaseLabel}" Text="{Binding Stunde}" FontSize="10"/> <Label Style="{StaticResource BaseLabel}" Text="{Binding Fach}" FontSize="10"/> <Label Style="{StaticResource BaseLabel}" Text="{Binding Raum}" FontSize="10"/> <Label Style="{StaticResource BaseLabel}" Text="{Binding Lehrer}" FontSize="10"/> <Label Style="{StaticResource BaseLabel}" Text="{Binding Art}" FontSize="10"/> </StackLayout> </Frame> </StackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> </RefreshView>
XAML.cs代码
TodayViewModel _viewModel; public TodayPage() { InitializeComponent(); BindingContext = _viewModel = new TodayViewModel(); } protected override void OnAppearing() { base.OnAppearing(); _viewModel.OnAppearing(); }
ViewModel代码
public Command LoadStandInsCommand { get; } public ObservableCollection<StandIn> StandIns { get; } public TodayViewModel() { StandIns = new ObservableCollection<StandIn>(); LoadStandInsCommand = new Command(async ()=> await ExecuteLoadStandInsCommand()); } async Task ExecuteLoadStandInsCommand() { IsBusy = true; try { StandIns.Clear(); if (loadListToday) { sIList.AddRange(gh.GetStandInsToday()); } else { sIList.AddRange(gh.GetStandInsTomorrow()); } foreach (StandIn item in sIList) { if (item.Klasse == pN.Klasse || item.Lehrer == pN.Klasse) { StandIns.Add(item); } } } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsBusy = false; } }
排查与解决建议
确保ViewModel实现属性变更通知
你的ViewModel未展示INotifyPropertyChanged实现代码,IsBusy属性需要通过该接口通知UI更新,否则RefreshView状态异常可能影响CollectionView加载。建议继承ObservableObject(来自CommunityToolkit.Mvvm)简化实现:public class TodayViewModel : ObservableObject { private bool _isBusy; public bool IsBusy { get => _isBusy; set => SetProperty(ref _isBusy, value); } // 其他现有代码 }确认OnAppearing触发数据加载
检查ViewModel的OnAppearing方法是否调用了LoadStandInsCommand.Execute(null),如果没有,数据不会主动加载:public void OnAppearing() { LoadStandInsCommand.Execute(null); }移除ScrollView嵌套
CollectionView自带滚动功能,与ScrollView嵌套会导致布局计算异常,直接保留RefreshView包裹CollectionView即可。验证StandIn类属性访问权限
确保StandIn的Stunde、Fach等属性为public,否则绑定无法读取数据:public class StandIn { public string Stunde { get; set; } public string Fach { get; set; } // 其他属性同理设置为public }检查数据过滤逻辑
在ExecuteLoadStandInsCommand中添加调试输出,确认筛选后StandIns的实际数量,排查item.Klasse == pN.Klasse || item.Lehrer == pN.Klasse条件是否符合预期:Debug.WriteLine($"筛选后数据量:{StandIns.Count}");调整布局约束
Frame设置的HeightRequest="50"可能限制内容显示,暂时移除该属性,或检查BaseLabel样式是否存在文字颜色与背景色一致的问题,导致内容不可见。
内容的提问来源于stack exchange,提问作者tengelberth

