我用这段代码动画我的窗口:
winLogin login = new winLogin();
login.Owner = this;
login.Show();
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = this.Left + ((this.Width - login.Width) / 2);
da.AutoReverse = false;
da.Duration = new Duration(TimeSpan.FromSeconds(0.1));
login.BeginAnimation(Window.LeftProperty, da);
Run Code Online (Sandbox Code Playgroud)
问题是,无论何时我设置Left此窗口的属性(在动画之后),它都会变得疯狂.
我使用此代码将子窗口始终放在中心,但Left我使用动画的窗口的属性无法正确更改.
private void Window_LocationChanged(object sender, EventArgs e)
{
foreach (Window win in this.OwnedWindows)
{
win.Top = this.Top + ((this.Height - win.Height) / 2);
win.Left = this.Left + ((this.Width - win.Width) / 2);
}
}
Run Code Online (Sandbox Code Playgroud)
首先,当您设置动画时,应始终删除该属性的潜在先前动画:
login.BeginAnimation(Window.LeftProperty, null);
login.BeginAnimation(Window.LeftProperty, da);
Run Code Online (Sandbox Code Playgroud)
如果你不这样做,你将得到内存泄漏,可能还有其他一些不良行为.
同样由于DependencyProperty优先级,您无法在具有活动动画的DependecyProperty上设置值,因为它的FillBehavior设置为HoldEnd(默认值),因此动画就是这种情况.你必须首先删除动画.