items not showing in mvxlistview

2019-02-27 07:58发布

问题:

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

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 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?

Thanks

Amit

回答1:

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