gpt4 book ai didi

c# - XAML : Setting the value of Storyboard animation from code behind

转载 作者:行者123 更新时间:2023-11-30 20:53:42 26 4
gpt4 key购买 nike

我有一个要求,我需要一个按钮在几秒钟内从屏幕的一端移动到另一端(水平)。我试过这个并且在某种程度上有效。

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

但问题是由于屏幕尺寸和屏幕方向。 在大屏幕上它停在中间。我想动态设置EasingDoubleKeyFrameValue。代码隐藏或绑定(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com