0

我正在使用两个故事板向左和向右移动球(图像),在左键的点击事件中我想向左移动球,在右键的点击事件中,球应该向右移动。它在点击左右按钮时可以正常工作,但在加载应用程序故事板时会自动开始,并且球会在不点击按钮的情况下移动一步。如何限制情节提要自动开始?

这是我第一个将球向左移动 40 像素的故事板:

<EventTrigger>
<BeginStoryboard>
<Storyboard x:Name="ballstoryleft">
<DoubleAnimation  Storyboard.TargetName="balltrans" Storyboard.TargetProperty="TranslateX"
Duration="0:0:0.5" By="-40" FillBehavior="HoldEnd">
<DoubleAnimation.EasingFunction>
<ElasticEase Oscillations="0" Springiness="1" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>              

这是我的第二个故事板,将球向右移动 40 像素:

<EventTrigger>
<BeginStoryboard>
<Storyboard x:Name="ballstoryright">
<DoubleAnimation  Storyboard.TargetName="balltrans" Storyboard.TargetProperty="TranslateX"
         Duration="0:0:0.5" By="40"    FillBehavior="HoldEnd">
<DoubleAnimation.EasingFunction>
<ElasticEase Oscillations="0" Springiness="1" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>

我在后面的代码中编写了这段代码:

private void imgleft_Tapped(object sender, TappedRoutedEventArgs e)
{
ballstoryleft.Begin();
}
private void imgright_Tapped(object sender, TappedRoutedEventArgs e)
{
ballstoryright.Begin();
}
4

2 回答 2

1

你把你的放在哪里了Storyboards?我看到他们在里面EventTriggers。它们可能是在应用程序加载时触发情节提要的那些。

你应该把两者都Storyboards放在一个Resources集合中:

<Page.Resources>
    <Storyboard x:Name="ballstoryleft">
        <DoubleAnimation Storyboard.TargetName="balltrans" Storyboard.TargetProperty="TranslateX" 
                         Duration="0:0:0.5" By="-40" FillBehavior="HoldEnd">
            <DoubleAnimation.EasingFunction>
                <ElasticEase Oscillations="0" Springiness="1" />
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>
    <Storyboard x:Name="ballstoryright">
        <DoubleAnimation Storyboard.TargetName="balltrans" Storyboard.TargetProperty="TranslateX" 
                         Duration="0:0:0.5" By="40" FillBehavior="HoldEnd">
            <DoubleAnimation.EasingFunction>
                <ElasticEase Oscillations="0" Springiness="1" />
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>
</Page.Resources>

现在应该没有理由让它们自动触发,而您在点击时触发它们的代码应该仍然像现在一样工作。

于 2013-03-21T05:53:50.743 回答
0

您也可以从后面的代码中定义整个故事板

试试这个例子:

void OnButtonClick(object sender, RoutedEventArgs args) 
{ 
  DoubleAnimation anima = new DoubleAnimation
   { 
     EnableDependentAnimation = true,
     To = 96, 
     Duration = new Duration(new TimeSpan(0, 0, 1)), 
     AutoReverse = true, 
     RepeatBehavior = new RepeatBehavior(3) };
     Storyboard.SetTarget(anima, sender as Button);
     Storyboard.SetTargetProperty(anima, "FontSize");
     Storyboard storyboard = new Storyboard();
     storyboard.Children.Add(anima); 
     storyboard.Begin();
  }
于 2013-03-21T06:57:48.507 回答