私はWPFアプリケーションで作業しています。 MainWindow.xaml
に「Status_label」というラベルがあります。そのコンテンツを別のクラス(signIn.cs)から変更したいと考えています。 通常、私はこの別のクラスのWPFメインウィンドウのラベルを変更し、別のスレッド
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(() =>{..........
などで解決できますか?
EDIT: 私はつもりコール別のクラスからこのラベル変更アクションだとしてウェル-として別のスレッド
MainWindow.xaml
<Label HorizontalAlignment="Left" Margin="14,312,0,0" Name="status_lable" Width="361"/>
SignIn.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"));
}
l INEするvarのMainWinのリターンエラーThe calling thread cannot access this object because a different thread owns it.
が
なぜこれがダウン投票されたのですか? – iJay
おそらく、この質問は時間が浪費されているので答えました。いくつかの「グーグル」はあなたに適切な解決策を提供します。 – DHN