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();
}