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

标签 c# storyboard windows-store-apps winrt-xaml

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

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

相关文章:

ios - 自动布局:标题 View 逐渐包含 UILabel + UIButton

c# - 在 Windows 应用商店应用程序中无需用户干预即可打印

windows-8 - Windows 8 商店 appId 受哪些规则管辖?它们在版本/重命名中是否持久?

c# - 报告完成后共享目标契约(Contract)崩溃

c# - Java 等价于 C# async/await?

c# - 从 Task.WhenAll 获取结果

ios - 从通知重定向到另一个 Storyboard?

c# - MVC3、Razor 和 EF5 : Child properties not displayed

c# - 16 字节的 Rijndael 初始化向量

ios - prepareforsegue() 未执行(swift/storyboards)