我有一个自定义UserControl,它实现了以下动画:
<UserControl.Resources>
<QuinticEase Core:Key="EasingInOut" EasingMode="EaseInOut"/>
<Storyboard Core:Key="AnimationHide">
<DoubleAnimation Storyboard.TargetName="m_Front" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)" BeginTime="00:00:00.0" Duration="00:00:00.1" EasingFunction="{StaticResource EasingInOut}" To="0"/>
<DoubleAnimation Storyboard.TargetName="m_Back" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)" BeginTime="00:00:00.1" Duration="00:00:00.1" EasingFunction="{StaticResource EasingInOut}" To="1"/>
</Storyboard>
<Storyboard Core:Key="AnimationShow">
<DoubleAnimation Storyboard.TargetName="m_Back" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)" BeginTime="00:00:00.0" Duration="00:00:00.1" EasingFunction="{StaticResource EasingInOut}" To="0"/>
<DoubleAnimation Storyboard.TargetName="m_Front" Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleX)" BeginTime="00:00:00.1" Duration="00:00:00.1" EasingFunction="{StaticResource EasingInOut}" To="1"/>
</Storyboard>
</UserControl.Resources>
具有以下结构:
<Canvas>
<Grid Core:Name="m_Front" Height="{Core:Static Namespace:Card.RealHeight}" RenderTransformOrigin="0.5,0.5" Width="{Core:Static Namespace:Card.RealWidth}">
<Grid.RenderTransform>
<ScaleTransform ScaleX="0"/>
</Grid.RenderTransform>
<Image Core:Name="m_FrontImage" RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" HorizontalAlignment="Left" Source="{Binding Source={Core:Static Properties:Resources.Fronts}, Converter={StaticResource ImagesConverter}}" Stretch="None" VerticalAlignment="Top">
<Image.Clip>
<RectangleGeometry Core:Name="m_FrontClipping"/>
</Image.Clip>
<Image.RenderTransform>
<TranslateTransform Core:Name="m_FrontTranslation"/>
</Image.RenderTransform>
</Image>
</Grid>
<Grid Core:Name="m_Back" Canvas.Left="0" Canvas.Top="0" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<ScaleTransform/>
</Grid.RenderTransform>
<Image RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" HorizontalAlignment="Left" Source="{Binding Source={Core:Static Properties:Resources.Back}, Converter={StaticResource ImagesConverter}}" Stretch="None" VerticalAlignment="Top"/>
</Grid>
</Canvas>
如果我在UserControl代码中运行其中一个动画,那么一切都可以正常工作:
Storyboard show = (Storyboard)Resources["AnimationShow"];
show.Begin();
现在......我试图从UserControl本身外部模拟该动画(在我的MainWindow.cs代码后面确切地说):
DoubleAnimation animationKick1 = new DoubleAnimation();
animationKick1.BeginTime = beginTime;
animationKick1.Duration = TimeSpan.FromSeconds(0.125D);
animationKick1.To = 0D;
animationKick1.SetValue(Storyboard.TargetProperty, myUC.m_Front);
animationKick1.SetValue(Storyboard.TargetPropertyProperty, CreatePropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
DoubleAnimation animationKick2 = new DoubleAnimation();
animationKick2.BeginTime = beginTime;
animationKick2.Duration = TimeSpan.FromSeconds(0.125D);
animationKick2.To = 1D;
animationKick2.SetValue(Storyboard.TargetProperty, myUC.m_Back);
animationKick2.SetValue(Storyboard.TargetPropertyProperty, CreatePropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
但没有任何反应......为什么?如何从外部为UserControl元素设置动画?
答案 0 :(得分:2)
最好只在UserControl上公开一个方法来运行动画。您可能遇到了与属性路径有关的一些范围问题。尝试从主窗口访问用户控件的内部并不真正遵循封装的基本原则。是否有令人信服的理由将动画代码保存在MainWindow中?