ウィンドウの高さと幅を上下のプロパティと一緒にアニメーション化できません。
(これを実行したい場合は、サンプルをダウンロードするためのリンクがこの質問の最後にあります。)
このアニメーションを実行するときは常に、ウィンドウは最初に新しい場所にスムーズに移動し(上と左にスムーズに変更)、ウィンドウの移動中に幅プロパティをスムーズにアニメーション化します..しかし、ウィンドウの高さは応答しません..その後、ウィンドウの高さは 1 つの値(古い高さ)からジャンプします)新しい高さに。
私のWPFアプリケーションは簡単です-これをテストするためだけです(4つのプロパティ、1つのボタン、1つの機能):
まず、このプロパティをウィンドウに追加しました (ファイル: MainWindow.xaml.cs
):
/// <summary>
/// Position to lock for change size in horizontal way (x)
/// </summary>
public static readonly DependencyProperty LocationLockHorizontalProperty = DependencyProperty.Register("LocationLockHorizontal", typeof(AlignmentX), typeof(Window));
/// <summary>
/// Position to lock for change size in vertical way (y)
/// </summary>
public static readonly DependencyProperty LocationLockVerticalProperty = DependencyProperty.Register("LocationLockVertical", typeof(AlignmentY), typeof(Window));
/// <summary>
/// Location to lock for change size.
/// </summary>
public AlignmentX LocationLockHorizontal
{
get { return (AlignmentX)GetValue(LocationLockHorizontalProperty); }
set { SetValue(LocationLockHorizontalProperty, value); }
}
/// <summary>
/// Location to lock for change size.
/// </summary>
public AlignmentY LocationLockVertical
{
get { return (AlignmentY)GetValue(LocationLockVerticalProperty); }
set { SetValue(LocationLockVerticalProperty, value); }
}
次に、同じファイルに、アニメーションを作成して開始するための新しい手順を 1 つ追加しました。
public void AnimateResize(double changeWidth = 0d, double changeHeight = 0d, double durationMilisec = 200.0)
{
Storyboard sb = new Storyboard();
DoubleAnimation daw;
DoubleAnimation dah;
// animate window width
if (changeWidth != 0.0)
{
daw = new DoubleAnimation();
daw.From = this.ActualWidth;
daw.To = this.ActualWidth + changeWidth;
daw.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
daw.AccelerationRatio = 0.4;
daw.DecelerationRatio = 0.6;
// this.BeginAnimation(Window.WidthProperty, daw);
Storyboard.SetTarget(daw, this);
Storyboard.SetTargetProperty(daw, new PropertyPath(Window.WidthProperty));
sb.Children.Add(daw);
}
// animate window height
if (changeHeight != 0.0)
{
dah = new DoubleAnimation();
dah.From = this.ActualHeight;
dah.To = this.ActualHeight + changeHeight;
dah.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
dah.AccelerationRatio = 0.4;
dah.DecelerationRatio = 0.6;
Storyboard.SetTarget(dah, this);
Storyboard.SetTargetProperty(dah, new PropertyPath(Window.HeightProperty));
sb.Children.Add(dah); // this.BeginAnimation(Window.HeightProperty, dah);
}
DoubleAnimation dax;
DoubleAnimation day;
// animate window move in horizontal way
if (LocationLockHorizontal == AlignmentX.Center || LocationLockHorizontal == AlignmentX.Right)
{
dax = new DoubleAnimation();
dax.From = this.Left;
switch (LocationLockHorizontal)
{
case AlignmentX.Center:
dax.To = this.Left - changeWidth / 2.0;
break;
case AlignmentX.Right:
dax.To = this.Left - changeWidth;
break;
}
dax.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
dax.AccelerationRatio = 0.4; dax.DecelerationRatio = 0.6;
Storyboard.SetTarget(dax, this);
Storyboard.SetTargetProperty(dax, new PropertyPath(Window.LeftProperty));
sb.Children.Add(dax); // this.BeginAnimation(Window.LeftProperty, dax);
}
// animate window move vertical
if (LocationLockVertical == AlignmentY.Center || LocationLockVertical == AlignmentY.Bottom)
{
day = new DoubleAnimation();
day.From = this.Top;
switch (LocationLockVertical)
{
case AlignmentY.Center:
day.To = this.Top - changeHeight / 2.0;
break;
case AlignmentY.Bottom:
day.To = this.Top - changeHeight;
break;
}
day.Duration = new Duration(TimeSpan.FromMilliseconds(durationMilisec));
day.AccelerationRatio = 0.4; day.DecelerationRatio = 0.6;
Storyboard.SetTarget(day, this);
Storyboard.SetTargetProperty(day, new PropertyPath(Window.TopProperty));
sb.Children.Add(day); // this.BeginAnimation(Window.TopProperty, day);
}
sb.Begin();
}
MyWindow
同じファイルの初期化では、MainWindow.xaml.cs
プロパティは次の値に設定されます (アニメーションを垂直方向の底点と水平方向の中央に固定するため):
public MainWindow()
{
InitializeComponent();
LocationLockHorizontal = AlignmentX.Center; // fix center point when animate resizing
LocationLockVertical = AlignmentY.Bottom; // lock bottom corner of application
}
私のアプリケーションには1つのボタンがあり、そのために前の手順のランダム呼び出しのクリックイベントを設定しました:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.AnimateResize(new Random().NextDouble() * 400.0 - 200.0, new Random().NextDouble() * 500.0 - 250.0, 1000.0);
}
完全な例については、ここに私の xaml がありますMainWindow.xaml
:
<Window x:Class="Test_HeightAndTopAnimation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="300" WindowStartupLocation="CenterScreen">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
いくつかの記事 ( Wpf - Animate height from bottom up - I don`t have a grid 、またはWPF: Animation is not Smooth ) を読みましたが、この問題に関連するものは何もありません。
例を含む zip ファイルへのリンク: Google ドライブのアプリケーション コード
最後の注意: 私は Windows7 64bit と .NET 4.5 を使用しています