C# ShowDialog和Show有时会通过动画关闭窗口引发异常

C# ShowDialog和Show有时会通过动画关闭窗口引发异常,c#,wpf,windows,storyboard,C#,Wpf,Windows,Storyboard,我正在为几个朋友创建一个小应用程序。应用程序已经完成,但我想让它的GUI更“专业”,所以我要为它制作一个新的GUI。 我偶然发现了一个奇怪的问题,我使用其中一个对话框进行数据输入,有时我对其使用Close()函数时,showDialog函数有时会抛出一个InvalidOperationException,文本为“此可视化未连接到PresentationSource”。 如果我在try-catch和catch-the-exception中编写ShowDialog函数调用,以防止它停止应用程序运行时

我正在为几个朋友创建一个小应用程序。应用程序已经完成,但我想让它的GUI更“专业”,所以我要为它制作一个新的GUI。 我偶然发现了一个奇怪的问题,我使用其中一个对话框进行数据输入,有时我对其使用Close()函数时,showDialog函数有时会抛出一个InvalidOperationException,文本为“此可视化未连接到PresentationSource”。 如果我在try-catch和catch-the-exception中编写ShowDialog函数调用,以防止它停止应用程序运行时,它工作正常,相关窗口仍然可以按预期打开和关闭,但它有时会抛出该异常

作为对话框打开窗口的代码(单击按钮时从主窗口运行):

窗口类中的代码:

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();
}
按钮关闭窗口以触发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
System.Windows.Media.Visual.PointToScreen(Point Point)上的“堆栈跟踪”
StackTrace)\r\n
Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow.UpdatePlacement()\r\n位于
Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow.b_uu77_0(对象对象对象)\r\n位于
System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象args、Int32 numArgs)\r\n位于
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、Int32 numArgs、委托catchHandler)\r\n位于
System.Windows.Threading.DispatcherOperation.InvokeImpl()\r\n位于
System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态)\r\n位于
MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(对象obj)\r\n位于
System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保留SyncCTX)\r\n位于
System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保留SyncCTX)\r\n
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)\r\n
在MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext,ContextCallback回调,对象状态)\r\n
位于System.Windows.Threading.DispatcherOperation.Invoke()\r\n
位于System.Windows.Threading.Dispatcher.ProcessQueue()\r\n
在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、Boolean和handled)中\r\n
在MS.Win32.HwndWrapper.WndProc(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)\r\n
在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)上\r\n
在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象args、Int32 numArgs)上\r\n
位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、Int32 numArgs、委托catchHandler)\r\n位于
System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority,TimeSpan timeout,委托方法,对象参数,Int32 numArgs)\r\n位于MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)\r\n位于
MS.Win32.UnsafentiveMethods.DispatchMessage(MSG&MSG)\r\n位于
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)\r\n位于System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)\r\n
位于System.Windows.Window.ShowHelper(对象布尔框)\r\n
在System.Windows.Window.Show()上\r\n
在System.Windows.Window.ShowDialog()上\r\n
在Gigglerekt_Scenebot_v2_GUI_update.main window.btnAddCommand_单击D:\\Development\\C#\\ Gigglerekt Scenebot v2 GUI update\\Gigglerekt Scenebot v2 GUI update\\main window.xaml.cs:第84行\r\n at
System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventTargets RoutedEventTargets)\r\n位于
System.Windows.EventRoute.InvokeHandlerSiml(对象源,RoutedEventArgs参数,布尔值重新调用)\r\n位于
System.Windows.UIElement.RaiseEventImpl(DependencyObject发件人,RoutedEventArgs参数)\r\n位于System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)\r\n位于
System.Windows.Controls.Primitives.ButtonBase.OnClick()\r\n位于
System.Windows.Controls.Button.OnClick()\r\n位于
System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)\r\n位于
System.Windows.UIElement.OnMouseLeftButtonUpThunk(对象发送器,MouseButtonEventArgs e)\r\n位于
System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(委托genericHandler,对象genericTarget)\r\n位于
System.Windows.RoutedEventTargets.InvokeHandler(委托处理程序,对象目标)\r\n
位于System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventTargets RoutedEventTargets)\r\n位于
System.Windows.EventRoute.InvokeHandlerSiml(对象源,RoutedEventArgs参数,布尔值重新调用)\r\n位于
System.Windows.UIElement.ReraiseEvents(DependencyObject发件人、RoutedEventArgs参数、RoutedEvent newEvent)\r\n位于
System.Windows.UIElement.OnMouseUpThunk(对象发送器,鼠标按钮Eventargs e)\r\n位于
System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(委托genericHandler,对象genericTarget)\r\n位于
System.Windows.RoutedEventArgs.InvokeHandler(
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
win.Show(this);
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();
}