Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wpf动画一次关闭导致火灾关闭事件两次_C#_Wpf_Events_Animation - Fatal编程技术网

C# Wpf动画一次关闭导致火灾关闭事件两次

C# Wpf动画一次关闭导致火灾关闭事件两次,c#,wpf,events,animation,C#,Wpf,Events,Animation,我为我的应用程序窗口使用一个基本窗口类 BaseWindow main窗口 现在,我在BaseWindow类的OnClosing中编写了一个动画,如下所示: void AnimationWindowBase_Closing(object sender, EventArgs e) { if (!CloseAnimationIsDone) { ((CancelEventArgs) e).Cancel = true; var closeAnimati

我为我的应用程序窗口使用一个基本窗口类

  • BaseWindow
  • main窗口
现在,我在
BaseWindow
类的
OnClosing
中编写了一个动画,如下所示:

void AnimationWindowBase_Closing(object sender, EventArgs e)
{
    if (!CloseAnimationIsDone)
    {
        ((CancelEventArgs) e).Cancel = true;
        var closeAnimation1 = new DoubleAnimation
        {
            From = RestoreBounds.Top,
            To = RestoreBounds.Top + 10,
            Duration = new Duration(TimeSpan.FromMilliseconds(500))
        };
        closeAnimation1.Completed += (s, eArgs) =>
        {
            CloseAnimationIsDone = true;

            // This line cause fire close again in MainWindow class
            Close();
        };
        BeginAnimation(TopProperty, closeAnimation1);

        BeginAnimation(OpacityProperty, new DoubleAnimation
        {
            From = 1,
            To = 0,
            Duration = new Duration(TimeSpan.FromMilliseconds(500))
        });
    }
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    AppConfigs.SaveAll();
}
protected override void OnClosing(CancelEventArgs e)
{
    base.OnClosing(e);

    if (!e.Cancel)
        AppConfigs.SaveAll();
}
但是如果我在
main窗口中有一个
OnClosing
方法,如下所示:

void AnimationWindowBase_Closing(object sender, EventArgs e)
{
    if (!CloseAnimationIsDone)
    {
        ((CancelEventArgs) e).Cancel = true;
        var closeAnimation1 = new DoubleAnimation
        {
            From = RestoreBounds.Top,
            To = RestoreBounds.Top + 10,
            Duration = new Duration(TimeSpan.FromMilliseconds(500))
        };
        closeAnimation1.Completed += (s, eArgs) =>
        {
            CloseAnimationIsDone = true;

            // This line cause fire close again in MainWindow class
            Close();
        };
        BeginAnimation(TopProperty, closeAnimation1);

        BeginAnimation(OpacityProperty, new DoubleAnimation
        {
            From = 1,
            To = 0,
            Duration = new Duration(TimeSpan.FromMilliseconds(500))
        });
    }
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    AppConfigs.SaveAll();
}
protected override void OnClosing(CancelEventArgs e)
{
    base.OnClosing(e);

    if (!e.Cancel)
        AppConfigs.SaveAll();
}
然后设置将被保存2


如何用一种好的方法解决它?

不要订阅
关闭
事件,而是尝试重写
一旦关闭
方法。基本窗口的
OnClosing
可以与
AnimationWindowBase\u Closing
方法具有相同的代码。但在主窗口中,您可以这样做:

void AnimationWindowBase_Closing(object sender, EventArgs e)
{
    if (!CloseAnimationIsDone)
    {
        ((CancelEventArgs) e).Cancel = true;
        var closeAnimation1 = new DoubleAnimation
        {
            From = RestoreBounds.Top,
            To = RestoreBounds.Top + 10,
            Duration = new Duration(TimeSpan.FromMilliseconds(500))
        };
        closeAnimation1.Completed += (s, eArgs) =>
        {
            CloseAnimationIsDone = true;

            // This line cause fire close again in MainWindow class
            Close();
        };
        BeginAnimation(TopProperty, closeAnimation1);

        BeginAnimation(OpacityProperty, new DoubleAnimation
        {
            From = 1,
            To = 0,
            Duration = new Duration(TimeSpan.FromMilliseconds(500))
        });
    }
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    AppConfigs.SaveAll();
}
protected override void OnClosing(CancelEventArgs e)
{
    base.OnClosing(e);

    if (!e.Cancel)
        AppConfigs.SaveAll();
}