我有此方法可返回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中所有调度程序的数组或列表? 在此先感谢:)
答案 0 :(得分:0)
我最终将方法移至App.xaml.cs,然后使用以下方法调用了它:
App.Current.Dispatcher.Invoke(() =>
{
try
{
WindowController.MainPage.FriendsListTextBox.Document = SortFriendsList();
}
catch (Exception Exc)
{
Console.WriteLine(Exc.ToString());
}
});