InitializeComponent的InvalidOperationException

时间:2015-03-25 08:29:27

标签: c# wpf initialization invalidoperationexception

如果我从MainWindow类打开一个新窗口,我得到一个InvalidOperationException。调用InitializeComponent() - Method。

抛出异常

我不知道该怎么做。

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

第二次调用该方法时出现错误。

消息:

  

正在关闭Application对象。

     

来源:PresentationFramework

     

StackTrace:

     

在System.Windows.Application.GetResourcePackage(Uri packageUri)
     在System.Windows.Application.LoadComponent(对象组件,Uri   resourceLocator)
     在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 \ Project \ 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)Line 340   PresentationFramework.dll!System.Windows.Window.UpdateWindowListsOnClose()Line 4621
  PresentationFramework.dll!System.Windows.Window.InternalDispose()Line 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处理)Line 4349 + 0x7 Bytes
  PresentationCore.dll!System.Windows.Interop.HwndSource.PublicHooksFilterMessage(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam,ref bool处理)Line 1811 + 0x16 Bytes
  WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam,ref bool处理)Line 345 + 0x16 Bytes
  WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)行494 + 0x18字节
  WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback,object args,int numArgs)第111行+ 0xa字节
  WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,System.Delegate方法,对象args,int numArgs,System.Delegate catchHandler)第41行+ 0x17字节
  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
  [原产于管理过渡]
  [管理到原生过渡]
  WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam)Line 672 + 0x29 Bytes
  [原产于管理过渡]
  [管理到原生过渡]
  WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam)Line 424 + 0x1a Bytes
  [原产于管理过渡]
  [管理到原生过渡]
  WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam)Line 672 + 0x29 Bytes
  [原产于管理过渡]
  [管理到原生过渡]
  WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam)Line 424 + 0x1a Bytes
  [原产于管理过渡]
  [管理到原生过渡]
  WindowsBase.dll!MS.Win32.HwndSubclass.DefWndProcWrapper(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam)Line 672 + 0x29 Bytes
  [原产于管理过渡]
  [管理到原生过渡]
  WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam)Line 424 + 0x1a Bytes
  [原产于管理过渡]
  [管理到原生过渡]
  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()第211行+ 0x1d字节
  PresentationFramework.dll!System.Windows.Window.ShowDialog()第456行+ 0x7字节
  Test.exe!Test.FormMain.tab_Click(对象发送者,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)Line 278 + 0x9 Bytes
  System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)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)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
  [原产于管理过渡]
  [管理到原生过渡]
  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()第38行+ 0x1d字节
  [原产于管理过渡]
  [管理到原生过渡]
  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字节
  mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)第68行+ 0x27字节
  mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)581行+ 0xd字节
  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()第105行+ 0x20字节
  [原生于管理过渡]

第二次点击堆栈跟踪:

  

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()第1行+ 0xb字节
  Test.exe!Test.Testversandt.Testversandt(字符串html,字符串主题)第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)Line 278 + 0x9 Bytes
  System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)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)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
  [原产于管理过渡]
  [管理到原生过渡]
  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()第38行+ 0x1d字节
  [原产于管理过渡]
  [管理到原生过渡]
  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字节
  mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)第68行+ 0x27字节
  mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)581行+ 0xd字节
  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()第105行+ 0x20字节
  [原生于管理过渡]

编辑3

我找到了原因。

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

1 个答案:

答案 0 :(得分:2)

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

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

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

  3. 手动调用关机。

  4. 因此,请检查这种情况,因为它们会导致问题。

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