2017-10-05 112 views
0

我正在为几个朋友创建一个小应用程序。应用程序已经完成,但我想让它的GUI更“专业”,所以我正在为它制作一个新的GUI。 我偶然发现了一个奇怪的问题,其中一个对话框用于数据输入,有时当我使用Close()函数时,showDialog函数有时会抛出一个InvalidOperationException异常,并显示文本“This Visual is not connected to PresentationSource ”。 如果我在try catch中编写ShowDialog函数调用并捕获异常以防止它停止应用程序运行时,它将正常工作,并且所讨论的窗口仍然可以按预期打开和关闭,但有时会引发该异常。ShowDialog和Show有时会抛出异常动画关闭窗口

对(上按钮点击从主窗口运行)打开所述窗口作为对话的代码:在所述窗口类

addCommand win = new addCommand();  //Creates a new instance of the window 
win.Left = this.Left;     //Makes it appear on top of the mainwindow 
win.Top = this.Top + 25; 

((BlurEffect)appMain.Effect).Radius = 5;//Blurs out the background (Error occurs even with this not added 
win.ShowDialog();     //Shows window as dialog (This is where the exception is thrown 
((BlurEffect)appMain.Effect).Radius = 0;//Removes blur when window is closed 

代码:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    this.Close(); 
} 

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    if (!animationComplete) 
    { 
     Storyboard sb = Resources["closingAnimation"] as Storyboard; 
     sb.Begin(this); 
     e.Cancel = true; 
    } 
} 

private void storyboardComplete(object sender, EventArgs e) 
{ 
    animationComplete = true; 
    this.Close(); 
} 

A按钮关闭窗口触发的ShowDialog继续运行。如果动画未播放,则取消关闭并播放动画,在完成时将再次运行关闭,但将animationComplete设置为true并允许窗口关闭。动画是一个简单的双动画,将窗口的高度从92调整为0,以在窗口上创建“滑入,滑出”效果。

这有点像50/50。有时会起作用,有时会抛出异常。我不知道为什么它有时会抛出异常,有时不会。这似乎是随机的。

异常的堆栈跟踪

StackTrace " at System.Windows.Media.Visual.PointToScreen(Point point)\r\n at 
Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow.UpdatePlacement()\r\n at 
Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow.<UpdatePlacementAsync>b__77_0(Object obj)\r\n at 
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)\r\n at 
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)\r\n at 
System.Windows.Threading.DispatcherOperation.InvokeImpl()\r\n at 
System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)\r\n at 
MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)\r\n at 
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n at 
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n 
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)\r\n 
at System.Windows.Threading.DispatcherOperation.Invoke()\r\n 
at System.Windows.Threading.Dispatcher.ProcessQueue()\r\n 
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)\r\n 
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)\r\n 
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)\r\n 
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)\r\n 
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)\r\n at 
System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)\r\n at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n at 
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)\r\n at 
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)\r\n at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)\r\n 
at System.Windows.Window.ShowHelper(Object booleanBox)\r\n 
at System.Windows.Window.Show()\r\n 
at System.Windows.Window.ShowDialog()\r\n 
at Gigglerekt_Scenebot_v2_GUI_update.MainWindow.btnAddCommand_Click(Object sender, RoutedEventArgs e) in D:\\Development\\C#\\Gigglerekt Scenebot v2 GUI update\\Gigglerekt Scenebot v2 GUI update\\MainWindow.xaml.cs:line 84\r\n at 
System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)\r\n at 
System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)\r\n at 
System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)\r\n at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)\r\n at 
System.Windows.Controls.Primitives.ButtonBase.OnClick()\r\n at 
System.Windows.Controls.Button.OnClick()\r\n at 
System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)\r\n at 
System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)\r\n at 
System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)\r\n at 
System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)\r\n 
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)\r\n at 
System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)\r\n at 
System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)\r\n at 
System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)\r\n at 
System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)\r\n at 
System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)\r\n at 
System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)\r\n at 
System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)\r\n at 
System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)\r\n at 
System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)\r\n at 
System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)\r\n at System.Windows.Input.InputManager.ProcessStagingArea()\r\n at 
System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)\r\n at 
System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)\r\n at 
System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)\r\n at 
System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)\r\n at 
System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)\r\n at 
MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)\r\n at 
MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)\r\n at 
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)\r\n at 
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)\r\n at 
System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)\r\n at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n at 
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)\r\n at 
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)\r\n at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)\r\n at 
System.Windows.Application.RunDispatcher(Object ignore)\r\n at 
System.Windows.Application.RunInternal(Window window)\r\n at 
System.Windows.Application.Run(Window window)\r\n at 
System.Windows.Application.Run()\r\n at 
Gigglerekt_Scenebot_v2_GUI_update.App.Main()" string 

更新 WindowStyle设置为无,但如果我将它设置为别的东西(测试用工具窗口)异常看起来没有更多的再发生..测试看看我是否幸运或发生了什么

更新2它确实看起来像windowstyle没有设置为NONE时异常消失。这又不管我是否触发与Windows的X按钮或我已经添加的按钮,即关闭窗口

更新3如果我不要关上完成的动画窗口,隐藏它关闭动画。然后在恢复运行时(ShowDialog在隐藏窗口上继续显示)以及在父窗口关闭后,异常不再发生。所以,直到我找出为什么在动画完成后立即关闭它,并不是所有的时间都无法使用windowstyle,我想它是一种解决方法?因此,当WindowStyle设置为None时,与WindowStyle未设置为None时相比,Close()会发生什么问题?

+0

我敢打赌,当您尝试关闭对话框 –

+0

@ Aaron.S动画仍然在运行 - 但他处理在他form_closing事件处理程序。 – AgapwIesu

+0

等待动画完成,然后关闭对话框,我敢打赌,它都不会出现异常,但如果其完成动画之前先关闭,然后你会得到你的异常 –

回答

0

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.show(v=vs.110).aspx

win.Show(this); 

父(形式催生了对话)是不知道它被关闭,设置此对话框的家长会通知,它关闭了父。

+0

哇,我把它放在评论后。作为您的回答,抓住别人的建议并不酷! – AgapwIesu

+0

遗憾没刷新看到您的评论 –

+0

没有的ShowDialog的重载需要一个窗口作为参数,但我没有尝试设置与业主= win.owner这是因为我用它最初的定位,它什么也没做 – krt16

0

我假设Button_Click是关闭按钮的事件处理程序。 因此,改变你的代码是这样的:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
     Storyboard sb = Resources["closingAnimation"] as Storyboard; 
     sb.Completed += new EventHandler(storyboardComplete); 
     sb.Begin(this); 
} 

private void storyboardComplete(object sender, EventArgs e) 
{ 
    this.Close(); 
} 

,消除WINDOW_CLOSING事件处理程序。自从我使用WPF以来已经有一段时间了,所以我假设ShowDialog不会给你窗口右上角的“X”关闭按钮,所以你只需要关心你自己的关闭按钮。当他们按下该关闭按钮时要执行的操作是启动动画,而实际上它是动画的结尾,确实关闭了对话框,因此请仅在storyboardComplete事件中保留this.Close()

+0

这没有什么区别。我所展示的窗口的windowStyle设置为none,以便更容易地定制它以适应应用程序的其余部分。 – krt16

+0

你有什么事件连接到'storyboardComplete'方法。我假设你正在为'Completed'事件使用这个处理程序。 – AgapwIesu

+0

是的,这是故事板中的Completed事件。 – krt16

0

设置Windowstyle到任何东西,但没有允许窗口关闭而没有抛出异常,但是这会使理由让我摆在首位过时创造对话的窗口。

为了这个,我发现现在正在动画隐藏窗口和父窗口关闭它的ShowDialog恢复运行后的修复。即使WindowStyle设置为none,也不会发生异常。