![](/img/trans.png)
[英]Can't update a System.Web.UI.WebControls.Label's text from a thread event
[英]Can't update rich textbox from UI Thread..?
我有这个返回 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());
}
});
然而。 出于某种原因,我得到
抛出异常: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());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.