如何限制故事板在Windows 8商店应用程序中自动开始?

时间:2013-03-21 05:26:38

标签: c# windows-8 storyboard winrt-xaml

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

这是我向左移动球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();
}

2 个答案:

答案 0 :(得分: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>

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

答案 1 :(得分: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();
  }