2010-11-16 51 views
0

我试着简单地将故事板复制并粘贴到资源字典中,但它不起作用。那么,我怎样才能成功地将故事板动画移动到资源字典中,以便主xaml文件尽可能干净且可读?如何将silverlight故事板移动到resourceDictionary中?

这是我想对什么是不工作可能有助于进入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> 

回答

0

更多详细的代码示例,但乍一看你可能会得到一个错误因为Storyboard.TargetName无法解析(因为具有该名称的项目不再位于同一个文件中),所以尝试启动此故事板时会出现这种情况。

解决方法是在代码中设置Storyboard Target(与TargetName相对),也许在包含要设置动画的网格的对象的构造函数中设置。这可能是这样的:

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

你可能也不得不要么删除d:IsOptomized标签或确保该命名空间在您的资源字典的顶部定义。

值得一提的是,这使得您的Xaml稍微更具可读性,但代价更复杂,这可能不是您想要做的折衷。