无法从UI线程更新富文本框

问题描述

我有方法可返回FlowDocument。

new_string

我正在调用这样的方法来更新Rich文本框的文档:

    private static FlowDocument SortFriendsList()
    {
        FlowDocument DocumentToReturn = new FlowDocument();
        Paragraph CurrentParagraph = new Paragraph();
        #region Online status ellipse
        System.Windows.Shapes.Ellipse Onlinestatus = new System.Windows.Shapes.Ellipse();
        Onlinestatus.Height = 30;
        Onlinestatus.Width = 30;
        Onlinestatus.stroke = System.Windows.Media.Brushes.Black;
        Onlinestatus.strokeThickness = 1.5;
        Onlinestatus.Fill = System.Windows.Media.Brushes.Green;
        #endregion
        #region Offline status ellipse
        System.Windows.Shapes.Ellipse Offlinestatus = new System.Windows.Shapes.Ellipse();
        Offlinestatus.Height = 30;
        Offlinestatus.Width = 30;
        Offlinestatus.stroke = System.Windows.Media.Brushes.Black;
        Offlinestatus.strokeThickness = 1.5;
        Offlinestatus.Fill = System.Windows.Media.Brushes.Red;
        #endregion

        if (CurrentFriendsList.Count == 0)
        {
            Label TextToShow = new Label();
            TextToShow.FontSize = 16;
            TextToShow.Foreground = System.Windows.Media.Brushes.Black;
            TextToShow.Content = "Uh oh... It looks like you have no friends for Now :(";
            CurrentParagraph.Inlines.Add(TextToShow);
            CurrentParagraph.Inlines.Add(Environment.NewLine);
            DocumentToReturn.Blocks.Add(StatusParagraph);
            return DocumentToReturn;
        }
        else
        {
            foreach (string Friend in CurrentFriendsList)
            {

                if (CurrentOnlineUsers.Contains(Friend))
                {

                    CurrentParagraph.Inlines.Add(Onlinestatus);
                }
                else
                {

                    CurrentParagraph.Inlines.Add(Offlinestatus);
                }
                #region Text to add after status ellipse
                Label TextToShow = new Label();
                TextToShow.FontSize = 16;
                TextToShow.Foreground = System.Windows.Media.Brushes.Black;
                TextToShow.Content = Friend;
                #endregion
                CurrentParagraph.Inlines.Add(TextToShow);
                CurrentParagraph.Inlines.Add(Environment.NewLine);
                DocumentToReturn.Blocks.Add(StatusParagraph);
            }
            return DocumentToReturn;
        }
    }

但是。由于某种原因我得到了

引发的异常:WindowsBase.dll中的'system.invalidOperationException' system.invalidOperationException:调用线程无法访问此对象,因为另一个线程拥有它。

富文本框位于选项卡项中,我尝试使用它的调度程序来代替,但是没有运气。我也尝试过页面,窗口和富文本框的调度程序,但是没有运气。有没有办法找到它的调度员?也许是一种方法,它在尝试执行dispatcher.checkaccess时返回App.Current中所有调度程序的数组或列表? 在此先感谢:)

解决方法

我最终将方法移至App.xaml.cs,然后使用以下方法调用了它:

            App.Current.Dispatcher.Invoke(() => 
            {
                try
                {
                    WindowController.MainPage.FriendsListTextBox.Document = SortFriendsList();
                }
                catch (Exception Exc)
                {
                    Console.WriteLine(Exc.ToString());
                }
            });