【问题标题】:Xamarin - ListView Displaying Data on IOSXamarin - ListView 在 IOS 上显示数据
【发布时间】:2018-07-20 08:12:55
【问题描述】:

我的列表视图数据在 UWP 和 Android 上正确显示,但在 IOS 上不正确。我认为这与我正在使用的 OnAppearing 方法以及它在 IOS 上实际调用的时间有关。

当我导航到项目列表页面时,从我的仪表板中,ItemPage 后面有以下代码

public ItemPage(string category = null, bool fromDashboard = false)
{
    InitializeComponent();
    BindingContext = new ItemViewModel(category, fromDashboard);
}

protected override void OnAppearing()
{
    var vm = BindingContext as ItemViewModel;
    vm?.GetData();
    base.OnAppearing();
}

然后在我的ItemViewModel 类中,我有一个构造函数,它接受categoryfromDashboard 标志并设置一些属性。

ItemViewModelGetData() 调用如下:

public void GetData()
{
    this.Items = this.RefreshData(this.ItemService.GetAll(this.SelectedCategory));

    if (this.FromDashboard)
        this.Items = this.Items.Where(x => x.Status != "Complete").ToObservableCollection();
}

最后ItemPage.xaml 有一个ListViewItemsSource="{Binding Items}"

由于某种原因,这在 Windows 和 Android 上运行良好,但在 IOS 上不会显示任何数据。页面是在 OnAppearing 方法之前渲染的还是什么?

【问题讨论】:

  • 你有没有试过在设置项后在GetData方法中调用RaisePropertyChanged方法?
  • 我所有的 ViewModel 都继承自 BaseViewModel,该 BaseViewModel 具有来自 INotifyPropertyChanged 的​​ OnPropertyChanged 方法。我需要手动调用吗?
  • 是的,你需要在某个地方手动调用它。
  • 现在将它添加到 GetData 方法的末尾。我会告诉你进展如何。

标签: c# ios xamarin viewmodel


【解决方案1】:

在代码中编辑 ViewModel 属性的值时,您必须调用 OnPropertyChanged 方法(来自 INotifyPropertyChanged 接口)让 UI 知道它必须更新。

像这样:

public void GetData()
{
    this.Items = this.RefreshData(this.ItemService.GetAll(this.SelectedCategory));

    if (this.FromDashboard)
        this.Items = this.Items.Where(x => x.Status != "Complete").ToObservableCollection();

    OnPropertyChanged(nameof(this.Items));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2021-08-26
    • 2021-05-22
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多