我有一个要求,我需要一个按钮在几秒钟内从屏幕的一端移动到另一端(水平)。我尝试过这个并且在一定程度上有效。
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="button">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" />
<!--<EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="{Binding MainPage.width}">-->
<EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="-1283.725">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="button">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"/>
<!--<EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="{Binding Path=MainPage.height}">-->
<EasingDoubleKeyFrame KeyTime="0:0:6.9" Value="-467.732">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseIn"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
但问题是由于屏幕尺寸和屏幕方向造成的。 在大屏幕上它停在中间。我想动态设置EasingDoubleKeyFrame的值。代码隐藏或绑定(bind)或任何其他方式。
但我不想在代码隐藏中编写整个 Storyboard。
我可以使用
获取屏幕宽度和高度Rect bounds = Window.Current.Bounds;
static double height = 0;
static double width = 0;
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
height = bounds.Height;
width = -1 * bounds.Width;
}
请告诉我是否有简单的方法。
最佳答案
我找不到绑定(bind) Storyboard表达式的值的方法。
但是,解决方法是每次都手动设置。我们可以通过给它一个名称属性来做到这一点,例如:
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform). (CompositeTransform.TranslateX)" Storyboard.TargetName="button">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" />
<EasingDoubleKeyFrame x:Name="buttonTranslateX" KeyTime="0:0:9.9" Value="0" />
</DoubleAnimationUsingKeyFrames>
在页面初始化上设置值,如下所示:
Rect bounds = Window.Current.Bounds;
static double height = 0;
static double width = 0;
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
height = bounds.Height;
width = -1 * bounds.Width;
buttonTranslateX.Value = width;
}
这将创建一个漂亮的小动画,其中对象将在大约 9 秒内水平移动穿过屏幕。
关于c# - XAML : Setting the value of Storyboard animation from code behind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762406/