【问题标题】:InvalidOperationException by InitializeComponentInitializeComponent 引发的 InvalidOperationException
【发布时间】:2015-05-28 19:58:41
【问题描述】:

如果我从 MainWindow 类打开一个新窗口,我会收到 InvalidOperationException。通过调用 InitializeComponent()-Method 抛出异常。

我不知道该怎么办。

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

第二次调用方法发生错误。

消息:

Application 对象正在关闭。

来源:PresentationFramework

堆栈跟踪:

在 System.Windows.Application.GetResourcePackage(Uri packageUri)
在 System.Windows.Application.LoadComponent(对象组件,Uri 资源定位器)
在 Project.View.ItemWindow.InitializeComponent() 中 d:\Documents\Visual Studio 2012\Projects\Project\View\ItemWindow.xaml:第 1 行。
在 d:\Documents\Visual Studio 中的 Project.View.ItemWindow..ctor() 2012\Projects\ 项目 \View\ItemWindow.xaml.cs:第 45 行。
在 Project.View.EditButton_OnClick(对象发送者,RoutedEventArgs e) 在 d:\Documents\Visual Studio 2012\Projects\Project \View\MainWindow.xaml.cs:479 行。

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

Edit2:问题不仅出现在调试模式,也出现在发布模式。

第一次点击时的堆栈跟踪:

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

第二次点击的堆栈跟踪:

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

编辑 3

我找到了原因。

主应用程序是用 Windows 窗体编写的。在选项卡中,应用程序使用 WPF-UserControl 执行 elementhost。此用户控件打开 WPF 窗口。通过关闭此 WPF-Window,WPF-UserControl 也将关闭。 (严重停机)

【问题讨论】:

  • 粘贴完整的错误信息。
  • 您是否尝试过打开没有预定义 startlocation 和 topmost 属性的窗口?
  • 您需要为您的问题添加更多详细信息
  • 检查内部异常。通常它缺少 xaml 中 DynamicResource 引用的资源,但它可能是加载 xaml 时导致的任何异常,例如viewmodel ctor 中的错误。
  • @horHAY,没有效果

标签: c# wpf initialization invalidoperationexception


【解决方案1】:

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

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

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

  3. 手动调用关机。

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

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

【讨论】:

  • 我没有关闭窗口的函数调用或事件
  • 好的。要检查没有人调用 shutdown 添加一个新断点。为此,请打开调试选项(菜单调试/选项和设置)并选中“启用 .Net 框架源步进”。在System.Windows.Application.CriticalShutdown 添加一个新断点(菜单Debug/New Breakpoint/Break at Function)。启动您的应用程序并重复操作以重现错误。当调试器命中断点时,复制 stactrace 并将其添加到您的问题中。
  • 现在我的可执行程序中出现了同样的错误,不仅仅是在调试模式下。调试器命中System.Windows.Application.CriticalShutdown,但我看不到任何堆栈跟踪。
  • 您真的在调用堆栈窗口中看不到堆栈跟踪吗?
  • 因为您在某处创建了Application。要知道在哪里,请在System.Windows.Application.Application 处添加一个断点。或者在显示WPF之前使用Application.CurrentWindow获取当前实例并设置关闭模式。