在WPF中设置动画后无法设置属性 [英] Unable to set a property after its animation in WPF

查看:288
本文介绍了在WPF中设置动画后无法设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码对窗口进行了动画处理:

I used this code to animation my window:

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

问题是,每当我设置 Left

我使用此代码将子窗口始终对准中心,但我使用动画的窗口的左属性无法正确更改。

I used this code to align the child windows to be always on the center but the Left property of the windows on which i used an animation cannot be properly changed.

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


推荐答案

第一个总之,设置动画时,应始终删除该属性的潜在先前动画:

First of all, when you set an animation you should always remove the potential previous animation of that property:

login.BeginAnimation(Window.LeftProperty, null);
login.BeginAnimation(Window.LeftProperty, da);

如果不这样做,将会导致内存泄漏以及其他一些不良行为。

If you don't so this you will get a memory leak and probably some other undesired behavior.

也由于 DependencyProperty优先级,您不能在具有活动动画的DependecyProperty上设置值,因为动画 FillBehavior 设置为 HoldEnd (默认设置)。同样,您将必须先删除动画。

Also due to the DependencyProperty precedence you can not set a value on a DependecyProperty that has an active animation, wich is the case in your animation because its FillBehavior is set to HoldEnd (the default). Again you would have to remove the animation first.

这篇关于在WPF中设置动画后无法设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆