0
我正在尝试使用Storyboard
调整WPF窗口的大小。当我为窗口宽度或高度设置动画时,我的代码完美地工作,但是当我在它们上应用故事板时,它只动画第一个属性(在我的情况下为宽度)并跳过第二个属性。WPF Storyboard无法调整窗口宽度与高度
我的代码是:
private void AnimateWindowSize(double width, double height, bool isRelative = false)
{
//---------------------------------------------------
// Settings
//---------------------------------------------------
var duration = TimeSpan.FromSeconds(0.2);
var newWidth = isRelative ? this.Width - width : width;
var newHeight = isRelative ? this.Height - height : height;
Console.WriteLine($"Animating window from {this.Width}x{this.Height} to {newWidth}x{newHeight}");
this.Dispatcher.BeginInvoke(new Action(() =>
{
var storyboard = new Storyboard();
//---------------------------------------------------
// Animate Width
//---------------------------------------------------
var widthAnimation = new DoubleAnimation()
{
Duration = new Duration(duration),
From = this.ActualWidth,
To = newWidth
};
Storyboard.SetTarget(widthAnimation, this);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Window.WidthProperty));
storyboard.Children.Add(widthAnimation);
//---------------------------------------------------
// Animate Width
//---------------------------------------------------
var heightAnimation = new DoubleAnimation()
{
Duration = new Duration(duration),
From = this.ActualHeight,
To = newHeight
};
Storyboard.SetTarget(heightAnimation, this);
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Window.HeightProperty));
storyboard.Children.Add(heightAnimation);
//---------------------------------------------------
// Play
//---------------------------------------------------
//storyboard.Begin();
this.BeginStoryboard(storyboard, HandoffBehavior.SnapshotAndReplace, false);
}), null);
}
顺便说一句,我创造了这个方法的通用版本的动画菜单,我等的大小和它完美的作品。这似乎只是Window动画的一个问题。有谁知道如何修复它并为窗口宽度和高度应用动画?我在SO之后跟着答案,但所有这些都与Window之外的其他元素有关。
谢谢:)。移除heightAnimaion的持续时间确实会导致2动画执行,但它们不会并行运行,因此宽度和高度将同时调整大小。当我移除窗口的时间长度时,调整其宽度,然后调整其高度。 – OzB
嗨,你不能动画的宽度和高度并行,在这里你可以找到一个解决方法https://stackoverflow.com/questions/12332385/animating-a-wpf-window-width-and-height – Schnatti