首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Uwp中从代码后台创建故事板

在Uwp中从代码后台创建故事板
EN

Stack Overflow用户
提问于 2020-04-27 20:29:13
回答 1查看 40关注 0票数 0

我想从代码背后创建一个故事板,我想从代码背后实现下面的故事板,

代码语言:javascript
复制
 <Storyboard x:Name="Counter_Animation">
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtCounter" Storyboard.TargetProperty="(TextBlock.Text)">
            <DiscreteObjectKeyFrame KeyTime="00:00:01" Value="2"/>
            <DiscreteObjectKeyFrame KeyTime="00:00:02" Value="1"/>
            <DiscreteObjectKeyFrame KeyTime="00:00:03" Value="0"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>

在我的页面上有一个名为txtCounter的TextBlock,我试图通过代码实现,如下所示,

代码语言:javascript
复制
private void CounterAnimation()
    {

        Storyboard storyboard = new Storyboard();
        ObjectAnimationUsingKeyFrames anim = new ObjectAnimationUsingKeyFrames();
        anim.BeginTime = TimeSpan.FromSeconds(0);
        DiscreteObjectKeyFrame keyFrames = new DiscreteObjectKeyFrame();
        keyFrames.KeyTime = TimeSpan.FromSeconds(1);
        keyFrames.Value = "2";
        keyFrames.KeyTime = TimeSpan.FromSeconds(2);
        keyFrames.Value = "1";
        keyFrames.KeyTime = TimeSpan.FromSeconds(3);
        keyFrames.Value = "0";
        anim.KeyFrames.Add(keyFrames);

        Storyboard.SetTarget(anim, txtCounter);
        Storyboard.SetTargetProperty(anim, "Text");

        storyboard.Children.Add(anim);

        storyboard.Begin();
    }

代码编译良好并运行的问题是,在关键时间1 txtCounter应该显示2,在关键时间2 txtCounter应该显示1,在关键时间3 txtCounter应该显示0,但它直接显示0(即不显示中间值)。如果我弄错了,请看一下并纠正我。这将help.Thanks

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-27 21:37:47

只是在这里写完整的代码,如果有人有更好的想法,那么请建议这对我来说是可行的,

代码语言:javascript
复制
private Storyboard GetCounterAnimation()
    {

        Storyboard storyboard = new Storyboard();
        ObjectAnimationUsingKeyFrames counterAnimation = new ObjectAnimationUsingKeyFrames
        {
            BeginTime = TimeSpan.FromSeconds(0),
        };
        counterAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = TimeSpan.FromSeconds(1), Value = "2" });
        counterAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = TimeSpan.FromSeconds(2), Value = "1" });
        counterAnimation.KeyFrames.Add(new DiscreteObjectKeyFrame { KeyTime = TimeSpan.FromSeconds(3), Value = "Inhale" });
        Storyboard.SetTarget(counterAnimation, txtCounter);
        Storyboard.SetTargetProperty(counterAnimation, "Text");
        storyboard.Children.Add(counterAnimation);
        return storyboard;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61458946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档