WP7 - 动画结束得太早并且没有效果

发布于 2024-12-01 19:36:55 字数 1455 浏览 1 评论 0原文

我制作了一个动画来旋转我的控件,但是当它触发时,它只是跳到最终位置,然后立即返回到起始位置。

控件是由后面的代码组成的,是具有以下属性的文本框:

FontSize = 45;
TextAlignment = TextAlignment.Center;
Widh = 40;
Heigh = 45;

我将文本框存储在数组中,因此我使用以下代码触发动画:

foreach (TextBlock b in arrayOfTextBoxes)
{
    Rotate(b);
}

动画:

public static void Rotate(UIElement target)
{
        var projection = new PlaneProjection();
        target.Projection = projection;

        DoubleAnimation da = new DoubleAnimation();
        da.From = 0;
        da.To = 90;
        da.Duration = TimeSpan.FromSeconds(0.25);
        da.AutoReverse = false;

        Storyboard.SetTargetProperty(da, new PropertyPath(PlaneProjection.RotationZProperty));
        Storyboard.SetTarget(da, projection);

        Storyboard sb = new Storyboard();
        sb.Children.Add(da);

        EventHandler eh = null;
        eh = (s, args) =>
        {
            projection.RotationZ = 90;
            sb.Stop();
            sb.Completed -= eh;
        };
        sb.Completed += eh;

        sb.Begin();
}

编辑:现在我大概知道什么是问题。我每秒都会有一个(有时是两个)调度程序时间在后台滴答作响。当我禁用它们时,问题就消失了。但我做不到,因为我需要它们来测量时间。 在我触发动画之前停止它们并再次启动它们并没有帮助。 //DispetcherTimers 执行以下操作:

        void tikac_Tick(object sender, EventArgs e)
    {
        herniCas += new TimeSpan(0, 0, 1);
    }

I made an animation to rotate my control, but when it's trigert it just jumps to final position and then immediately returns to starting position.

The Controls are made from code behind and are TextBoxes with following properties:

FontSize = 45;
TextAlignment = TextAlignment.Center;
Widh = 40;
Heigh = 45;

I store the TextBoxes in array, so I trigger the animation with following code:

foreach (TextBlock b in arrayOfTextBoxes)
{
    Rotate(b);
}

Animation:

public static void Rotate(UIElement target)
{
        var projection = new PlaneProjection();
        target.Projection = projection;

        DoubleAnimation da = new DoubleAnimation();
        da.From = 0;
        da.To = 90;
        da.Duration = TimeSpan.FromSeconds(0.25);
        da.AutoReverse = false;

        Storyboard.SetTargetProperty(da, new PropertyPath(PlaneProjection.RotationZProperty));
        Storyboard.SetTarget(da, projection);

        Storyboard sb = new Storyboard();
        sb.Children.Add(da);

        EventHandler eh = null;
        eh = (s, args) =>
        {
            projection.RotationZ = 90;
            sb.Stop();
            sb.Completed -= eh;
        };
        sb.Completed += eh;

        sb.Begin();
}

EDIT: Now i propably know what's the problem. I have one (sometimes two) Dispatcher Times ticking in background every second. When I disable them, the proble's gone. But I can't do it because I need them to measure time.
Stoping them just before I trigger the animation and them starting them again does'n help.
//The DispetcherTimers do this:

        void tikac_Tick(object sender, EventArgs e)
    {
        herniCas += new TimeSpan(0, 0, 1);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脸赞 2024-12-08 19:36:55

动画故事板通常会保留最后一个值“直到它们停止”。它们不会永久改变实际财产价值。它们只是更改其当前呈现的值(依赖属性不是很棒吗?:))

当您在结束时显式停止它时,该值将恢复为其原始值。如果您希望值保持更改,请不要停止故事板,或者在动画更改时在代码中显式设置它。

您的示例中的动画速度非常快,很快就结束了(故事板的启动时间可以是几帧,并且中间的任何时间都会被跳过,因为动画是固定时间插值,而不是特定于帧的)。

Animation storyboards will normally hold the last value "until they are stopped". They do not permanently change the actual property value. They just change its currently rendered value (aren't dependency properties wonderful? :))

As you are explicitly stopping it when it ends the value reverts to its original value. Do not stop the storyboard if you want the value to stay changed, or explicitly set it in code when the animation changes.

The animation in your example is so fast it is just ending very quickly (startup time for storyboards can be a few frames and any time in between is skipped as animations are fixed-time interpolations, not frame specific).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文