【发布时间】:2022-01-21 23:55:40
【问题描述】:
我正在尝试在 xaml 中显示字典的所有键/值对,但我收到在视图模型中找不到属性“键”的错误。 “Statistics”的绑定是字典类型,所以我不确定我做错了什么。
这是我在 xaml 中的代码。它在第 11 行给出了错误。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Student.Mobile.Views.StatisticsPage"
xmlns:local="clr-namespace:Student.Mobile.ViewModels">
<ContentPage.Content>
<RefreshView x:DataType="local:StatisticsViewModel" Command="{Binding LoadStatisticsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
<ListView ItemsSource="{Binding Statistics}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Key, StringFormat='Week: {0}'}" Detail="{Binding Value, StringFormat='Aantal uren gestudeerd: {0}'}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</RefreshView>
</ContentPage.Content>
</ContentPage>
统计信息存在于视图模型中。如果有人能把我引向正确的方向,我会很高兴;)
public class StatisticsViewModel : BaseViewModel
{
private readonly IToastService _toastService;
private readonly INavigationService _navigationService;
private readonly IStatisticsService _statisticsService;
public Command LoadStatisticsCommand { get; }
public Dictionary<string, double> Statistics { get; set; }
public StatisticsViewModel(IToastService toastService, INavigationService navigationService, IStatisticsService statisticsService)
{
_toastService = toastService;
_navigationService = navigationService;
_statisticsService = statisticsService;
Title = "Statistics";
Statistics = null;
LoadStatisticsCommand = new Command(async () => await ExecuteLoadStatisticsCommand());
}
正在 ExecuteLoadStatistisCommand 中设置统计信息。
提前致谢!
【问题讨论】:
-
绑定到视图时属性是否填充?
-
@Isparia 该属性在构造函数中设置为 null,但在 LoadStatisticsCommand 中从后端检索。
-
一般我没有看到错误,你在执行命令时引发了你的propertychanged事件吗?
-
大声笑不,我没有。我应该实现 INotifyPropertyChanged 接口,对吗?很久没用了。
-
是的,这将是正确的方法,据我所知 ^^ 但可观察的集合也应该像你在回答中所说的那样工作
标签: c# xaml xamarin xamarin.forms data-binding