从外部动画UserControl元素
本文关键字:元素 UserControl 动画 从外部 | 更新日期: 2023-09-27 18:03:02
我有一个自定义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元素?
你最好只是在你的UserControl上暴露一个方法来运行动画。您可能会遇到一些属性路径的范围问题。试图从主窗口访问用户控件的内部并没有真正遵循封装的基本原则。是否有令人信服的理由将动画代码保留在主窗口中?