asynchronous UI update from ViewModel in WPF

后端 未结 2 669
忘掉有多难
忘掉有多难 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 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 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();
                         }));
            }
        }
    }
    

提交回复
热议问题