【问题标题】:Xamarin Forms ListView inside TabbedPage not update using ObservableCollectionTabbedPage 内的 Xamarin Forms ListView 未使用 ObservableCollection 更新
【发布时间】:2020-06-12 11:39:28
【问题描述】:

我的XAML 文件的ListView 被一个ViewModel 填充,该ViewModel 具有来自服务的ObservableCollection,但ListView 没有更新信息。我已经检查了该服务是否返回了正确的信息。

XML:

<ListView x:Name="DashboardDetailsList" 
            SelectionMode="None" 
            HasUnevenRows="True" 
            ItemsSource="{Binding DetailsList}"
            BackgroundColor="Transparent" 
            VerticalOptions="FillAndExpand" 
            SeparatorVisibility="None">
   <ListView.ItemTemplate>
       <DataTemplate>
           <ViewCell>
              ...
              ...
           </ViewCell>
       </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

代码:

public ObservableCollection<Dashboard> DetailsList { get; set; } = new ObservableCollection<Dashboard>();
//API Call
details = await _clientAPI.getDashboardDetails(id);
if (details != null)
{
    DetailsList.Clear();
    foreach (var item in details)
    {
        DetailsList.Add(item);
    }
}

【问题讨论】:

  • 使用 InotifyPropertyChanged() 将其设为提升属性
  • 什么时候设置ViewModel为Page的BindingContext?将 DetailsList 转换为完整属性并调用 OnPropertyChanged 将是更安全的代码。
  • 你好,@enish 请给我示例代码。
  • 您可以编辑问题并发布整个 XAML 及其背后的代码吗?呼应@enisn - 我也有预感您没有设置ContentPage.BindingContext,因为您的ViewModel 逻辑很好,并且您正确地将ListView.ItemSource 绑定到ObservableCollection

标签: listview mvvm xamarin.forms observablecollection tabbedpage


【解决方案1】:

试试这个人

public ObservableCollection<Dashboard> DetailsList { get; set; } = new ObservableCollection<Dashboard>();
//API Call
details = await _clientAPI.getDashboardDetails(id);
if (details != null)
{
    DetailsList = details;
}

【讨论】:

    【解决方案2】:

    对不起,伙计..您在使用 MVVM 吗?

    您需要通知变量已更改。

    这样

    private ObservableCollection <Dashboard> _detailsList;
    public ObservableCollection <Dashboard> DetailsList
    {
        get => _detailsList;
        private set => SetProperty (ref _detailsList, value);
    }
    
    //API Call
    details = await _clientAPI.getDashboardDetails(id);
    if (details != null)
    {
        DetailsList = details;
    }
    

    【讨论】:

    猜你喜欢
    • 2016-10-03
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2021-03-21
    相关资源
    最近更新 更多