我正在开发一个 WPF 应用程序。我有一个名为“Status_label”的标签MainWindow.xaml
。我想从不同的类(signIn.cs)更改它的内容。通常我可以做到这一点
var mainWin = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
mainWin.status_lable.Content = "Irantha signed in";
但我的问题是,当我尝试通过 signIn.cs 类中的不同线程访问它时,它给出了一个错误:
The calling thread cannot access this object because a different thread owns it.
我可以通过使用Dispatcher.Invoke(new Action(() =>{..........
或其他方式解决这个问题吗?
编辑: 我将从不同的类以及单独的线程中调用此标签更改操作
主窗口.xaml
<Label HorizontalAlignment="Left" Margin="14,312,0,0" Name="status_lable" Width="361"/>
登录.cs
internal void getStudentAttendence()
{
Thread captureFingerPrints = new Thread(startCapturing);
captureFingerPrints.Start();
}
void mySeparateThreadMethod()
{
var mainWin = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
mainWin.status_lable.Dispatcher.Invoke(new Action(()=> mainWin.status_lable.Content ="Irantha signed in"));
}
line var mainWin 返回错误The calling thread cannot access this object because a different thread owns it.
请指导我,
谢谢