WPF故事板错误

时间:2012-08-18 01:04:47

标签: wpf

我在窗口的资源中定义了故事板。当我在代码隐藏文件中访问这个storyboad时出现以下错误

“'此'名称在MainWindow的名称范围内找不到”

我的代码是

     Storyboard sb = this.Resources["transferToBasketStoryboard"] as Storyboard
    sb.Begin();

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

验证您是否在window.resources范围内创建了此故事板,以便MainWindow.Xaml应该以:

开头
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns: ..... 
    x:Class="myWpfApplication.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>// the resource should be here
    <Storyboard x:Key="transferToBasketStoryboard">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="textBlock">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="60"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>