我正在使用WPF,在我的主窗口上,每当我尝试做任何事情时它都会给我这个错误"调用线程无法访问此对象,因为另一个线程拥有它." 这些例如,
this.Visibility = Visibility.Visible ((Storyboard)this.FindResource("HideWindow")).Begin()
我真的不知道那是什么,并且在大量需要帮助时我无法执行我的代码的第一行.更多信息:我无法打开另一个窗口,给我的线程不是STA错误
LoginWindow objLoginWindow = new LoginWindow(); objLoginWindow.ShowDialog(); objLoginWindow = null;
这是例外.
System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=The calling thread cannot access this object because a different thread owns it. Source=WindowsBase StackTrace: at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at Menupedia.LoadingWindow.OpenNewWindow() in c:\Users\xxxx\Desktop\menupedia\Menupediaa\Menupedia\LoadingWindow.xaml.cs:line 51 at Menupedia.LoadingWindow.TimeElapsed(Object sender, ElapsedEventArgs e) in c:\Users\xxxx\Desktop\menupedia\Menupediaa\Menupedia\LoadingWindow.xaml.cs:line 39 at System.Timers.Timer.MyTimerCallback(Object state)
Clemens.. 6
您的堆栈跟踪显示您使用了System.Timers.Timer
.Elapsed
Timer 的处理程序在后台线程中调用,后者不允许直接访问UI元素.您必须调用Dispatcher.Invoke
或Dispatcher.BeginInvoke
访问Elapsed
处理程序中的UI元素.
但是,通过a替换Timer可能更容易System.Windows.Threading.DispatcherTimer
,它具有Tick
在UI线程中调用的事件,因此允许安全访问UI元素.
您的堆栈跟踪显示您使用了System.Timers.Timer
.Elapsed
Timer 的处理程序在后台线程中调用,后者不允许直接访问UI元素.您必须调用Dispatcher.Invoke
或Dispatcher.BeginInvoke
访问Elapsed
处理程序中的UI元素.
但是,通过a替换Timer可能更容易System.Windows.Threading.DispatcherTimer
,它具有Tick
在UI线程中调用的事件,因此允许安全访问UI元素.