You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI XAML页面如何遍历Dictionary类型ObservableCollection

实现方案

你当前的绑定失败有两个核心原因:

  1. 集合定义冗余:你把整个Dictionary作为单个元素塞进了ObservableCollection<Dictionary<...>>,平白多了一层无效嵌套
  2. 类型不匹配:Dictionary直接作为集合源遍历的时候,每一项是KeyValuePair<TKey, TValue>结构,不是单独的字符串Key,你之前设置的DataType和绑定路径完全不匹配
    另外你原XAML里第二层CollectionView的属性名写错了:ItemSource应为ItemsSource,拼写错误也会导致绑定失效。

第一步:修正ViewModel层集合逻辑

删掉多余的嵌套,直接把字典的键值对存入ObservableCollection即可:

// 修正集合类型,元素为字典的键值对结构
public ObservableCollection<KeyValuePair<string, List<OpenJob>>> OpenJobsCollection { get; } = new();

// 数据拉取后的赋值逻辑
try 
{
    IsBusy = true;
    var openJobs = await jobsService.getOpenJobs();
    
    OpenJobsCollection.Clear();
    // 如需按年月排序,在这里加OrderBy逻辑即可,例:OrderByDescending(k => k.Key)
    foreach(var kv in openJobs)
    {
        OpenJobsCollection.Add(kv);
    }
}

第二步:修正XAML绑定层级

KeyValuePair结构自带两个属性:Key对应你存的年月字符串,Value对应月份下的OpenJob列表,直接绑定这两个属性即可,完整XAML如下:

<!-- 头部记得加命名空间声明
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:models="clr-namespace:你的项目命名空间.Models"
-->
<VerticalStackLayout>
    <CollectionView
        ItemsSource="{Binding OpenJobsCollection}"
        SelectionMode="None">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="system:KeyValuePair<string, List<models:OpenJob>>">
                <Frame Padding="12">
                    <VerticalStackLayout Spacing="8">
                        <!-- 绑定Key显示月份标题 -->
                        <Label Text="{Binding Key}" FontAttributes="Bold" FontSize="Title"/>
                        <!-- 绑定Value作为月份下的岗位列表源 -->
                        <CollectionView ItemsSource="{Binding Value}">
                            <CollectionView.ItemTemplate>
                                <DataTemplate x:DataType="models:OpenJob">
                                    <VerticalStackLayout Padding="8,4">
                                        <Label Text="{Binding event_title}"/>
                                        <!-- 如需遍历岗位下的positions,继续嵌套CollectionView即可 -->
                                        <CollectionView ItemsSource="{Binding positions}">
                                            <CollectionView.ItemTemplate>
                                                <DataTemplate x:DataType="models:Position">
                                                    <Label Text="{Binding position_title}" FontSize="Caption" TextColor="Gray"/>
                                                </DataTemplate>
                                            </CollectionView.ItemTemplate>
                                        </CollectionView>
                                    </VerticalStackLayout>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                    </VerticalStackLayout>
                </Frame>
            </DataTemplate>
        </CollectionView.ItemTemplate>
     </CollectionView>
    
    <Button Text="Get Open Jobs"
            Command="{Binding GetOpenJobsCommand}"
            IsEnabled="{Binding IsNotBusy}"
            Margin="0,12"/>
    <ActivityIndicator IsVisible="{Binding IsBusy}"
                       IsRunning="{Binding IsBusy}"
                       HorizontalOptions="FillAndExpand"
                       VerticalOptions="CenterAndExpand"
                       />
</VerticalStackLayout>

注意事项

  • 反序列化前确认JSON格式正确:你贴的样例JSON中"event_title": "asdf"行尾缺少逗号,会导致反序列化失败
  • Dictionary本身不保证遍历顺序,如果需要固定按年月排序,在foreach循环前加排序逻辑即可
  • 编译绑定要求x:DataType类型完全匹配,必须在XAML头部声明system和models对应的命名空间,否则会报编译错误

内容的提问来源于stack exchange,提问作者silversunhunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 22:33:24