asynchronous UI update from ViewModel in WPF

后端 未结 2 665
忘掉有多难
忘掉有多难 2021-01-03 02:27

I am having a problem with getting data from db and showing in UI asynchronously. I am using MVVM light, when I click the button, action is triggered in ViewModel:



        
相关标签:
2条回答
  • 2021-01-03 02:52

    You haven't shown any of the code that's actually running on the background thread on completion but I'm guessing that in it you're creating a collection object that you're then trying to assign to your CollectionView. When the CV tries to update (on the UI thread) from your Refresh call it would then try to use the collection that's owned by the other thread.

    If you include the relevant code it would be easier to say for sure.

    0 讨论(0)
  • 2021-01-03 03:06

    You are creating CollectionViewSource in one thread and refreshing that in another thread (dispatcher thread). Update your GetFriendsListCompleted to

    private void GetFriendsListCompleted(object sender, ResultsArgs<Friend> e)
    {
        if (!e.HasError)
        {
            var curr = e.Results;
            if (curr != null)
            {
                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
                        () => {
                         this.FriendsList= new CollectionViewSource();
                         this.FriendsList.Source = list;
                         this.FriendsList.Filter += this.FriendFilter;
                         FilterText = "";
                         this.FriendsList.View.Refresh();
                         }));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题