如何迭代.NET MAUI页面XML中的字典观测
我有一个来自JSON的数据结构:
{
"07-2022": [],
"08-2022": [
{
"event_title": "asdf"
"positions": [
{
"position_title": "Graphics Op"
},
{
"position_title": "Graphic Design"
}
]
}
],
"06-2023": []
}
我在代码中有用于工作和位置的模型,
public class OpenJob {
// props...
public List<Position> positions { get; set; }
}
我获取数据并将其放入字典中
Dictionary<string, List<Models.OpenJob>> OpenJobList = new();
public async Task<Dictionary<string, List<Models.OpenJob>>> getOpenJobs() {
if (OpenJobList?.Count > 0) {
return OpenJobList;
}
var url = "https://api.com";
var response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode) {
//Debug.WriteLine(response.ToString);
OpenJobList = await response.Content.ReadFromJsonAsync<Dictionary<string, List<Models.OpenJob>>>();
}
return OpenJobList;
}
,然后在我的ViewModel中将其转移到
//...
public ObservableCollection<Dictionary<string, List<OpenJob>>> OpenJobsCollection { get; } = new();
//...
try {
IsBusy = true;
var openJobs = await jobsService.getOpenJobs();
if (OpenJobsCollection.Count > 0) {
OpenJobsCollection.Clear();
}
OpenJobsCollection.Add(openJobs);
}
XML页面中的观察力collection,我知道如何知道如何知道如何迭代一个简单的列表。但是我找不到有关此数据结构的解决方案。
VerticalStackLayout>
<CollectionView
ItemsSource="{This Should be the Dictionary}"
SelectionMode="None">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="THIS SHOULD BE THE STRING KEY">
<Frame>
<CollectionView
ItemSource="{This should be the List of OpenJob Models}" />
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="Models.OpenJob">
<Label Text="{Binding event_title}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="Get Open Jobs"
Command="{Binding GetOpenJobsCommand}"
IsEnabled="{Binding IsNotBusy}"
/>
<ActivityIndicator IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}"
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"
/>
</VerticalStackLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑一下您是否真的需要嵌套收集视图。通常有更好的解决方案,涉及不可滚动的布局。稍后我给你一个例子。
您在ViewModel中有三个嵌套集合(
observableCollection
,dictionary
和list&lt; openjob&gt;
),所以要小心,因为您的第一个视图中的布局必须绑定observableCollection
,而不是字典
。我给您的另一个建议是使用自定义类而不是
字典
,因为这允许您继续使用x:datatype
,而如果您继续使用字典,则要删除它,否则将生成编译时错误。您可以以这种方式在XAML中迭代字典:
如果您确实需要嵌套CollectionViews,则只需用另一个
verticalStackLayout
用另一个Collection> Collection> Collection> Collection view
替换> bindableLayout
带有经典的标签。Consider if you REALLY need to nest CollectionViews. There are often better solutions, involving not scrollable layouts. I'll give you an example of this later.
You have three nested collections in your viewmodel (the
ObservableCollection
, theDictionary
and theList<OpenJob>
), so be careful, because your first layout in the view has to bind theObservableCollection
, not theDictionary
.Another suggestion I would give you is to use a custom class instead of
Dictionary
, because that allows to you to keep usingx:DataType
, while if you keep using Dictionaries you have to remove it, otherwise a compile-time error will be generated.You can iterate over a Dictionary in xaml in this way:
If you really need to nest CollectionViews, you simply have to replace the outer
VerticalStackLayout
with anotherCollectionView
and replace theBindableLayout
tags with the classic ones.