项目未显示在 mvxlistview 中 [英] items not showing in mvxlistview

查看:44
本文介绍了项目未显示在 mvxlistview 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mvvmcross(非常有趣),但我在从 mvxlistview 添加和删除项目时一直遇到问题:

I am using mvvmcross (with great fun) but I keep having problems with adding and removing items from mvxlistview:

我的视图绑定到从 Web 服务器检索的项目列表,因此它在不同的线程中完成:

My View is binded to a List of items which are retrieved from a web server so it is done in a different thread:

async void  ActivateSearchInvoked ()
    {
        _activeSearchViewModel.IsLoading = true;
        await _activeSearchViewModel.Search (SearchString);
        _activeSearchViewModel.IsLoading = false;
    }   

Search 是一个调用 InnerSearch 的方法,这里是 View Model 中的代码

Search is a method which calls InnerSearch, Here is the code in the View Model

protected override Task InnerSearch ()
    {
        Users.Clear ();
        return Task.Factory.StartNew (SearchForUsers);
    }

    protected virtual void SearchForUsers()
    {
        int requestringUserID = AppConfiguration.Instance.User.ID;
        List<User> users = GetUsersFromWeb();
        if(users == null)
        {
            return;
        }
        foreach (var item in users)
        {
            Users.Add (new UserViewModel (item));
        }
        RaisePropertyChanged (() => Users);
    }

在屏幕刷新(例如旋转)之前,这似乎无法正常工作我是否遗漏了什么?

This does not seem to work properly until the screen is refreshed (for instance rotating it) Am I missing something?

谢谢

阿米特

推荐答案

除非您实际上使用不同的源列表或 INotifyCollectionChanged 支持集合,否则 MvxAdapter 将收到您的更改通知 - 但实际上不会知道它有任何工作要做.

Unless you are actually using a different source List or an INotifyCollectionChanged supporting collection, then the MvxAdapter will receive your change notification - but will not actually know it has any work to do.

为了解决这个问题,要么:

In order to work around this, either:

  • use a new List
  • switch to using an ObservableCollection
  • implement a custom IMvxAdapter which always responds to change notifications, even when it appears no change has happened. To do this, override if (_itemsSource == value) return; in SeItemsSource in https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxAdapter.cs

这篇关于项目未显示在 mvxlistview 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆