简体   繁体   English

Xamarin-在iOS上显示数据的ListView

[英]Xamarin - ListView Displaying Data on IOS

My list views data displays correctly on UWP and Android but not for IOS. 我的列表视图数据可以在UWP和Android上正确显示,但不能在IOS上正确显示。 I think it has something to do with the OnAppearing method I'm using and when it actually gets called on IOS. 我认为这与我正在使用的OnAppearing方法以及何时在IOS上实际调用它有关。

From my Dashboard when I navigate to an Item list page I have the following code behind on the ItemPage 在我的仪表板上,当我导航到“项目列表”页面时,我在ItemPageItemPage了以下代码

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();
}

Then in my ItemViewModel class I have a constructor that accepts a category and fromDashboard flag and sets some properties. 然后,在我的ItemViewModel类中,我有一个构造函数,该构造函数接受categoryfromDashboard标志并设置一些属性。

The ItemViewModel GetData() call is as follows: ItemViewModel GetData()调用如下:

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();
}

And then finally the ItemPage.xaml has a ListView with the ItemsSource="{Binding Items}" 最后, ItemPage.xaml具有带有ItemsSource="{Binding Items}"ListView

For some reason this works fine on Windows and Android but no data will show on IOS. 由于某种原因,这在Windows和Android上可以正常运行,但IOS上不会显示任何数据。 Is the page being rendered before the OnAppearing method or something? 是在OnAppearing方法之前呈现页面吗?

When editing values of ViewModel properties in the code, you have to call OnPropertyChanged method (from INotifyPropertyChanged interface) to let the UI know it has to update. 在代码中编辑ViewModel属性的值时,必须调用OnPropertyChanged方法(从INotifyPropertyChanged接口),以使UI知道它必须更新。

Like so: 像这样:

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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM