从代码隐藏动态访问故事板?

lha*_*han 1 c# silverlight storyboard windows-phone

我正在开发Windows Phone应用程序,但我相信这也适用Silverlight.我想创建一个Storyboard(现在我创造它在我的测试代码隐藏页的全局变量),其改变RectangleFill由透明属性设置为红色,然后回透明.我将Storyboard在多个矩形中重新使用它,所以我希望能够Begin()从代码隐藏中访问它.我找不到这样的例子,所以我用我认为可行的方法给了它一个镜头.

初始设置:

sb = new Storyboard();

turnOn= new ColorAnimation();
turnOn.From = Colors.Transparent;
turnOn.To = Colors.Red;
turnOn.Duration = new TimeSpan(0, 0, 0, 0, 500); //half second

turnOff= new ColorAnimation();
turnOff.From = Colors.Red;
turnOff.To = Colors.Transparent;
turnOff.Duration = new TimeSpan(0, 0, 0, 0, 500); //half second

Storyboard.SetTargetProperty(turnOn, new PropertyPath("(Rectangle.Fill)"));
Storyboard.SetTargetProperty(turnOff, new PropertyPath("(Rectangle.Fill)"));

sb.Children.Add(turnOn);
sb.Children.Add(turnOff);

canvas.Resources.Add("sb", sb); // this is the canvas where the rectangles will be
Run Code Online (Sandbox Code Playgroud)

然后,在任何时候,如果我有一个Rectangle实例,它将在Canvas上面,我想启动故事板 - 它会变成Rectangle红色,然后变回透明:

// Set the target to my current rectangle, and begin:
Storyboard.SetTarget(turnOn, myRectangle);
Storyboard.SetTarget(turnOn, myRectangle);
sb.Begin();
Run Code Online (Sandbox Code Playgroud)

但是,它会抛出以下异常sb.Begin():

用户代码未处理InvalidOperationException System.Windows.ni.dll中发生类型为"System.InvalidOperationException"的异常,但未在用户代码中处理

我希望我的思路并不太远,但也许有一种更简单的方法呢?如果没有,谁能看到我做错了什么?

更新(解决方案):

我按照@Josh Mackey提供的示例移动了我StoryboardXAML页面,我认为无论如何都会更干净.我之前没有注意到的是异常细节,指出"由于类型不兼容,"ColorAnimation不能用于动画属性填充." 我可以通过改变我ColorAnimation来使用@Josh Mackey的解决方案ColorAnimationUsingKeyFrames.新代码:

<Canvas.Resources>
  <Storyboard x:Name="sb">
    <ColorAnimationUsingKeyFrames 
      Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
         AutoReverse="True">
            <EasingColorKeyFrame KeyTime="00:00:0" Value="Black" />
            <EasingColorKeyFrame KeyTime="00:00:0.25" Value="Red" />
    </ColorAnimationUsingKeyFrames>
  </Storyboard>
</Canvas.Resources>
Run Code Online (Sandbox Code Playgroud)

我只能使用一个动画,因为我能够设置AutoReverseTrue,我发现这篇文章描述了我如何Storyboard在我的代码隐藏中访问如下:

sb.Stop();
sb.SetValue(Storyboard.TargetNameProperty, myRectangle.Name);
sb.Begin();
Run Code Online (Sandbox Code Playgroud)

现在它按预期工作!

小智 6

您无法在XAML文件中定义故事板的任何特殊原因?

以下示例来自我的WP7项目.它是一个用户控件,但是同样的过程适用,只需将UserControl替换为您正在使用的任何内容.

<UserControl.Resources>
    <Storyboard x:Key="aniFlipToBack">
        <DoubleAnimation Storyboard.TargetName="LayoutRoot" 
                         Completed="DoubleAnimation_Completed"
                         Storyboard.TargetProperty="(Grid.Projection).(PlaneProjection.RotationY)" 
                         From="0" To="180" Duration="0:0:0.5" RepeatBehavior="1x" />
        <ObjectAnimationUsingKeyFrames 
            Duration="0:0:0.5"
            Storyboard.TargetName="gridFront" Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="Collapsed" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames 
            Duration="0:0:0.5"
            Storyboard.TargetName="gridBack" Storyboard.TargetProperty="Visibility">
            <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="Visible" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>     
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

并使用它:

Storyboard storyBoard = (Storyboard)this.Resources["aniFlipToBack"];
storyBoard.Begin();
Run Code Online (Sandbox Code Playgroud)

(我会评论,但显然我的代表不够高,所以我会发一个样本的答案,使其有效.:P)