0

我有一个实现以下动画的自定义 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 元素设置动画?

4

1 回答 1

2

你最好只在你的 UserControl 上公开一个方法来运行动画。您的属性路径可能会遇到一些范围界定问题。尝试从主窗口访问用户控件的内部并不真正遵循封装的基本原则。是否有令人信服的理由将动画代码保留在 MainWindow 中?

于 2013-03-07T17:39:28.130 回答