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