【问题标题】:How to move a silverlight storyboard into a resourceDictionary?如何将 Silverlight 故事板移动到资源字典中?
【发布时间】:2011-05-10 18:42:51
【问题描述】:

我尝试简单地将故事板复制并粘贴到资源字典中,但它不起作用。那么我怎样才能成功地将情节提要动画移动到资源字典中,以使主 xaml 文件尽可能干净和可读?

这是我想移入 animationResource.xaml 的代码示例

<!-- the story board controlling the grid animation -->
<Storyboard x:Name="MoveBall">
        <DoubleAnimation Duration="0:0:1" To="200" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="grid" d:IsOptimized="True">
            <DoubleAnimation.EasingFunction>
                <BackEase EasingMode="EaseInOut"/>
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>


<!-- I've left out all the other default xaml like the layoutRoot grid --->

<Grid x:Name="grid" HorizontalAlignment="Left" Margin="190,180,0,220" Width="80" RenderTransformOrigin="0.5,0.5">
        <Grid.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
            </TransformGroup>
        </Grid.RenderTransform>
        <Ellipse Fill="#FFF4F4F5" Stroke="Black"/>
    </Grid>

【问题讨论】:

    标签: silverlight xaml animation storyboard


    【解决方案1】:

    更多关于究竟是什么不工作的细节可能会有所帮助,但乍一看,您可能会在尝试启动此故事板时遇到错误,因为 Storyboard.TargetName 无法解析(因为具有此名称的项目不再在同一个文件中)。

    解决方案是在代码中设置 Storyboard Target(而不是 TargetName),可能在包含您要设置动画的网格的对象的构造函数中。这可能看起来像:

    Storyboard.SetTarget(App.Current.Resources["MoveBall"] as Storyboard, grid);
    

    您可能还必须删除 d:IsOptomized 标记或确保在资源字典的顶部定义了此命名空间。

    值得一提的是,这会使您的 Xaml 更具可读性,但代价是使您的代码更复杂,这可能不是您想要做出的权衡。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 2013-02-02
      相关资源
      最近更新 更多