在特定时间使用故事板调用方法 [英] Invoke methods at specific times using a Storyboard

查看:122
本文介绍了在特定时间使用故事板调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Storyboard 对象在特定时间调用方法。特别;我试图做一个烟花显示,用 Firework 用户控件有一个 Fire 方法。我想创建几个这些控件,并在特定时间调用他们的 Fire 方法(就像一个真正的节目)。

I would like to utilize a Storyboard object to invoke methods at specific times. Specifically; I am trying to make a fireworks display, with a Firework user control that has a Fire method. I want to create several of these controls and call their Fire methods at specific times (just like a real show).

通常,您可以使用 Storyboard 来对一个对象的属性进行动画处理。 是否有方法调用方法?

Normally, you would use a Storyboard to animate properties of an object over time. Is there a way to invoke a method instead?

类似的代码(当然是伪代码):

Something like (pseudo-code of course):

<Storyboard>
    <MethodUsingKeyFrames>
         <MethodKeyFrame KeyTime="0:0:1" TargetName="Firework1" Method="Fire"/>
         <MethodKeyFrame KeyTime="0:0:2.5" TargetName="Firework2" Method="Fire"/>
    </MethodUsingKeyFrames>
</Storyboard>

我考虑过使用 Timer 一个高的间隔和做自己的测序,但这似乎是一个黑客;特别是当我们有这样一个很好的现有做法的方式。

I have considered just using a Timer on a high interval and doing the sequencing myself, but that seems like a hack; especially when we have such a nice existing way of doing it.

注意相关使用storyboard调用动态创建的用户控件中的方法;问题(在面值)似乎是一样的,但答案与我想要完成的无关。

Note: Related to Use a storyboard to call a method in a usercontrol created dynamically ; the question (at face value) seems to be the same, but the answer has nothing to do with what I am trying to accomplish.

推荐答案

以下是我最后使用的:

向用户控件添加依赖属性 NeedsToFire a bool )。然后使用以下函数处理 PropertyChanged 事件(通过元数据):

Add a dependency property NeedsToFire to the user control (a bool). Then handle the PropertyChanged event (via the metadata) with a function like:

public bool NeedsToFire
{
   get { return (bool)GetValue(NeedsToFireProperty); }
   set { SetValue(NeedsToFireProperty, value); }
}

public static readonly DependencyProperty NeedsToFireProperty =
        DependencyProperty.Register("NeedsToFire", typeof(bool), 
            typeof(Firework), new PropertyMetadata(false, HandleNeedsToFire));

private static void HandleNeedsToFire(DependencyObject d, 
                                      DependencyPropertyChangedEventArgs e)
{
   if ((bool)e.NewValue)
   {
      (d as Firework).Fire();
   }
}

最后,使用 BooleanAnimationUsingKeyFrames 在正确的时间设置此属性:

Finally, use a BooleanAnimationUsingKeyFrames to set this property at the correct time:

<Storyboard x:Key="FireworksShow">
    <BooleanAnimationUsingKeyFrames Storyboard.TargetName="Firework1" 
                                    Storyboard.TargetProperty="NeedsToFire">
         <DiscreteBooleanKeyFrame KeyTime="0:0:0.5"  Value="True" />
    </BooleanAnimationUsingKeyFrames>
</Storyboard>

这似乎是最容易实现的解决方案;即使它需要使用依赖属性,它们并不一定意图是这样。它还允许重复发射。

This seems like the easiest to implement solution; even if it does require using Dependency Properties in a way they weren't necessarily intended to be. It also allows for repeat firing.

这篇关于在特定时间使用故事板调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆