无法从 UI 主题更新富文本框..?
Can't update rich textbox from UI Thread..?
我有这个方法 return 是一个 FlowDocument。
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;
}
}
我正在调用这样的方法来更新富文本框的文档:
App.Current.Dispatcher.Invoke(() =>
{
try
{
WindowController.MainPage.FriendsListTextBox.Document = SortFriendsList();
}
catch (Exception Exc)
{
Console.WriteLine(Exc.ToString());
}
});
不过。出于某种原因,我得到
Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll
System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
富文本框位于选项卡项内,我尝试使用它的调度程序,但没有成功。我已经尝试过页面、window 和富文本框的调度程序,但都没有成功。有没有办法找到它的调度员?也许是 return App.Current 中所有调度程序的数组或列表同时尝试执行 dispatcher.checkaccess 的方法?
提前致谢:)
我最终将我的方法移动到 App.xaml.cs 然后调用它:
App.Current.Dispatcher.Invoke(() =>
{
try
{
WindowController.MainPage.FriendsListTextBox.Document = SortFriendsList();
}
catch (Exception Exc)
{
Console.WriteLine(Exc.ToString());
}
});
我有这个方法 return 是一个 FlowDocument。
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;
}
}
我正在调用这样的方法来更新富文本框的文档:
App.Current.Dispatcher.Invoke(() =>
{
try
{
WindowController.MainPage.FriendsListTextBox.Document = SortFriendsList();
}
catch (Exception Exc)
{
Console.WriteLine(Exc.ToString());
}
});
不过。出于某种原因,我得到
Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
富文本框位于选项卡项内,我尝试使用它的调度程序,但没有成功。我已经尝试过页面、window 和富文本框的调度程序,但都没有成功。有没有办法找到它的调度员?也许是 return App.Current 中所有调度程序的数组或列表同时尝试执行 dispatcher.checkaccess 的方法? 提前致谢:)
我最终将我的方法移动到 App.xaml.cs 然后调用它:
App.Current.Dispatcher.Invoke(() =>
{
try
{
WindowController.MainPage.FriendsListTextBox.Document = SortFriendsList();
}
catch (Exception Exc)
{
Console.WriteLine(Exc.ToString());
}
});