I've tried simply copying and pasting a storyboard into a resource dictionary but it does开发者_StackOverflown't work. So how can i successfully move a storyboard animation into a resource dictionary so that the main xaml file is as clean and readable as possible?
Here is an example of code i want to move into 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>
More detail on exactly what isn't working might be helpful, but at first glance you'll probably be getting an error when trying to start this storyboard because the Storyboard.TargetName can't resolve (since the item with this name is no longer in the same file).
A solution for this would be to set the Storyboard Target (as opposed to the TargetName) in code, perhaps in the constructor for the object containing the grid you want to animate. This might look like:
Storyboard.SetTarget(App.Current.Resources["MoveBall"] as Storyboard, grid);
You'll probably also have to either remove the d:IsOptomized tag or make sure that this namespace is defined at the top of your resource dictionary.
It bears mentioning that this makes your Xaml slightly more readable at the cost of making your code more complex, which might not be the kind of tradeoff you want to make.
精彩评论