从外部用户控件动画元素 [英] Animate UserControl Elements from Outside

查看:130
本文介绍了从外部用户控件动画元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现了以下的动画自定义的用户控件:

I have a custom UserControl which implements the following animations:

<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>

如果我跑在后面的一切在用户控件code这些动画作品之一精绝的:

If I run one of those animations in the UserControl code behind everything works absolutely fine:

Storyboard show = (Storyboard)Resources["AnimationShow"];
show.Begin();

现在......我试图从用户控件本身(在我MainWindow.cs code背后是精确的)外模拟该动画:

Now... I was trying to simulate that animation from outside the UserControl itself (in my MainWindow.cs code behind to be exact):

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)"));

但没有任何反应......为什么?我怎样才能从外部用户控件动画元素?

But nothing happens... why? How can I animate UserControl elements from outside?

推荐答案

你最好只露出你的用户控件运行动画的方法。你可能运行到与你的财产的路径一定范围的问题。试图从主窗口访问用户控件的内部并没有真正遵循封装的基本原理。是否有一个令人信服的理由,以保持动画code在主窗口?

You're better off just exposing a method on your UserControl to run the animation. You're probably running into some scoping issues with your property paths. Trying to access the internals of a user control from your main window doesn't really follow the basic principals of encapsulation. Is there a compelling reason to keep the animation code in MainWindow?

这篇关于从外部用户控件动画元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆