I've developed a WPF application which uses an advanced 3rd party UserControl; the UserControl shows an interactive map. If I open the main window of the application by specifying StartupUri="MainWindow"
in XAML, or if I start the application from code using app.Run(new MainWindow())
, then everything works fine. However, if I instead handle the Application Startup event this way
我开发了一个使用高级第三方UserControl的WPF应用程序; UserControl显示交互式地图。如果我通过在XAML中指定StartupUri =“MainWindow”来打开应用程序的主窗口,或者如果我使用app.Run(new MainWindow())从代码启动应用程序,那么一切正常。但是,如果我改为以这种方式处理Application Startup事件
private void Application_Startup(object sender, StartupEventArgs e)
{
new MainWindow().Show();
}
then I get the following exception when I start interacting with the map:
然后当我开始与地图交互时,我得到以下异常:
Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.
不能使用属于与其父Freezable不同的线程的DependencyObject。
Why might this happen - is there some difference in which thread does what between these different ways of starting the application? Here is a partial stack trace for the exception:
为什么会发生这种情况 - 哪种线程在启动应用程序的不同方式之间做了哪些不同?以下是异常的部分堆栈跟踪:
at System.Windows.Freezable.EnsureConsistentDispatchers(DependencyObject owner, DependencyObject child) at System.Windows.Freezable.OnFreezablePropertyChanged(DependencyObject oldValue, DependencyObject newValue, DependencyProperty property) at System.Windows.Media.RenderData.PropagateChangedHandler(EventHandler handler, Boolean adding) at System.Windows.Media.DrawingVisual.RenderClose(IDrawingContent newContent) at System.Windows.Media.RenderDataDrawingContext.DisposeCore() at System.Windows.Media.DrawingContext.System.IDisposable.Dispose() at some3rdPartyClass.BuildVisual(Brush fg, Brush bg)
1 个解决方案
#1
0
Assuming that the 3rd party (but obviously I can't test it) invoke a thread and that happens to be the UI one when the xaml is in the starturi ....
假设第三方(但显然我无法测试它)调用一个线程,当xaml在starturi时恰好是UI一个....
Try this
尝试这个
Application.Current.Dispatcher.Invoke(
new Action(() => {
//pls share what kind of arguments you're using...
new MainWindow().Show();
})
);
#1
0
Assuming that the 3rd party (but obviously I can't test it) invoke a thread and that happens to be the UI one when the xaml is in the starturi ....
假设第三方(但显然我无法测试它)调用一个线程,当xaml在starturi时恰好是UI一个....
Try this
尝试这个
Application.Current.Dispatcher.Invoke(
new Action(() => {
//pls share what kind of arguments you're using...
new MainWindow().Show();
})
);