InvalidOperationException 由 InitializeComponent

InvalidOperationException by InitializeComponent

如果我从 MainWindow Class 打开一个新的 Window,我得到一个 InvalidOperationException。通过调用 InitializeComponent()-Method 抛出异常。

我不知道该怎么办。

ItemWindow itemWindow = new ItemWindow()
{
   WindowStartupLocation = WindowStartupLocation.CenterScreen,
   Topmost = true
};
itemWindow.ShowDialog();

第二次调用方法出现错误

消息:

The Application object is being shut down.

Source: PresentationFramework

The StackTrace:

at System.Windows.Application.GetResourcePackage(Uri packageUri)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Project.View.ItemWindow.InitializeComponent() in d:\Documents\Visual Studio 2012\Projects\Project\View\ItemWindow.xaml:line 1.
at Project.View.ItemWindow..ctor() in d:\Documents\Visual Studio 2012\Projects\ Project \View\ItemWindow.xaml.cs:line 45.
at Project.View.EditButton_OnClick(Object sender, RoutedEventArgs e) in d:\Documents\Visual Studio 2012\Projects\ Project \View\MainWindow.xaml.cs:line 479.

编辑: 问题只出现在调试模式下。

Edit2: 这个问题不仅出现在debug模式,releas模式也有。

第一次点击堆栈跟踪:

PresentationFramework.dll!System.Windows.Application.CriticalShutdown(int exitCode) Line 340
PresentationFramework.dll!System.Windows.Window.UpdateWindowListsOnClose() Line 4621
PresentationFramework.dll!System.Windows.Window.InternalDispose() Line 2380
PresentationFramework.dll!System.Windows.Window.WmDestroy() Line 4660
PresentationFramework.dll!System.Windows.Window.WindowFilterMessage(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 4349 + 0x7 Bytes
PresentationCore.dll!System.Windows.Interop.HwndSource.PublicHooksFilterMessage(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 1811 + 0x16 Bytes
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Line 345 + 0x16 Bytes
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Line 494 + 0x18 Bytes
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Line 111 + 0xa Bytes
WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source, System.Delegate method, object args, int numArgs, System.Delegate catchHandler) Line 41 + 0x17 Bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Line 1447 + 0x2d Bytes
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 398
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 672 + 0x29 Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 424 + 0x1a Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 672 + 0x29 Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 424 + 0x1a Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 672 + 0x29 Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Line 424 + 0x1a Bytes
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Line 2281 + 0x38 Bytes
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame) Line 368 + 0x9 Bytes
PresentationFramework.dll!System.Windows.Window.ShowHelper(object booleanBox) Line 5745 + 0xb Bytes
PresentationFramework.dll!System.Windows.Window.Show() Line 211 + 0x1d Bytes
PresentationFramework.dll!System.Windows.Window.ShowDialog() Line 456 + 0x7 Bytes
Test.exe!Test.FormMain.tab_Click(object sender, System.EventArgs e) Line 405 + 0xd Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) Line 8504 + 0xf Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) Line 278 + 0x9 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent) Line 308
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) Line 13171 + 0x274 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) Line 13920
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) Line 1308 + 0xa Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) Line 397 + 0x7 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Line 14207 + 0xe Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Line 14262 + 0xb Bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Line 779 + 0xe Bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) Line 2082
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) Line 3422 + 0x1b Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Line 3306 + 0xc Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) Line 1495 + 0x31 Bytes
Test.exe!Test.Program.Main() Line 38 + 0x1d Bytes
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Line 2031 + 0x18 Bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) Line 68 + 0x27 Bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 581 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 530 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 519 + 0xe Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() Line 105 + 0x20 Bytes
[Native to Managed Transition]

第二次点击堆栈轨迹:

PresentationFramework.dll!System.Windows.Application.GetResourcePackage(System.Uri packageUri) Line 2278
PresentationFramework.dll!System.Windows.Application.GetResourceOrContentPart(System.Uri uri) Line 2256 + 0x14 Bytes
PresentationFramework.dll!System.Windows.Application.LoadComponent(object component, System.Uri resourceLocator) Line 486 + 0x16 Bytes
Test.exe!Test.Testversandt.InitializeComponent() Line 1 + 0xb Bytes
Test.exe!Test.Testversandt.Testversandt(string html, string subject) Line 27 + 0x8 Bytes
Test.exe!Test.FormMain.tab_Click(object sender, System.EventArgs e) Line 404 + 0x21 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventArgs e) Line 8504 + 0xf Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventArgs e) Line 278 + 0x9 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent) Line 308
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) Line 13171 + 0x274 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) Line 13920
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m) Line 1308 + 0xa Bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m) Line 397 + 0x7 Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Line 14207 + 0xe Bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Line 14262 + 0xb Bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Line 779 + 0xe Bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) Line 2082
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) Line 3422 + 0x1b Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Line 3306 + 0xc Bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form mainForm) Line 1495 + 0x31 Bytes
Test.exe!Test.Program.Main() Line 38 + 0x1d Bytes
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) Line 2031 + 0x18 Bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) Line 68 + 0x27 Bytes
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 581 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 530 + 0xd Bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 519 + 0xe Bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() Line 105 + 0x20 Bytes
[Native to Managed Transition]

编辑 3

找到原因了。

主应用程序以 windows 形式编写。在选项卡中,应用程序使用 WPF-UserControl 执行 elementhost。此 UserControl 打开 WPF-Window。通过关闭此 WPF-Window,WPF-UserControl 也将关闭。 (严重关机)

此消息表示您的应用程序开始关闭。关机可以通过以下方式启动:

  1. 如果 Application.ShutdownMode 等于 OnLastWindowClose,则关闭所有 windows。

  2. 如果 Application.ShutdownMode 等于 OnMainWindowClose,则关闭主 window。

  3. 正在手动调用关机。

所以检查这种情况,因为它们会导致问题。

编辑: 因为您有第一个列出的原因,所以您应该将 Application.ShutdownMode 更改为 OnExplicitShutdown