1

我已经创建了一个基本的微调器类型控件,它工作得相当好,但其中有大量重复的代码。除了两个属性之外,它基本上是一遍又一遍的相同元素,其中一个属性是情节提要的 BeginTime - 每一行只是无休止地重复其动画,但每一行在前一行之后开始 0.1 秒小波浪效应。

这是代码的总体思路:

<UserControl x:Class="MyNamespace.Spinner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Rectangle Fill="Black" Opacity="0.5"
                   Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=Width}"
                   Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=Height}" />
        <Line X1="9" X2="11" Y1="10" Y2="20">
            <Line.Triggers>
                <EventTrigger RoutedEvent="Line.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard RepeatBehavior="Forever" Duration="0:0:0.8"
                                        BeginTime="0:0:0.1">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="0%" />
                                    <LinearDoubleKeyFrame Value="1.0" KeyTime="12.5%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="75%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="100%" />
                                </DoubleAnimationUsingKeyFrames>
                             </Storyboard>
                         </BeginStoryboard>
                     </EventTrigger.Actions>
                 </EventTrigger>
             </Line.Triggers>
         </Line>
        <Line X1="14" X2="16" Y1="10" Y2="20">
            <Line.Triggers>
                <EventTrigger RoutedEvent="Line.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard RepeatBehavior="Forever" Duration="0:0:0.8"
                                        BeginTime="0:0:0.2">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="0%" />
                                    <LinearDoubleKeyFrame Value="1.0" KeyTime="12.5%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="75%" />
                                    <LinearDoubleKeyFrame Value="0.5" KeyTime="100%" />
                                </DoubleAnimationUsingKeyFrames>
                             </Storyboard>
                         </BeginStoryboard>
                     </EventTrigger.Actions>
                 </EventTrigger>
             </Line.Triggers>
         </Line>

    <!-- And so on and so forth, 6 more times -->
    </Grid> 
</UserControl>

我不想把整件事都贴出来,那可能看起来太痛苦了,但你明白了。从 Line.Triggers 到 /Line.Triggers 的唯一变化是情节提要的 BeginTime。XAML 中有没有一种方法可以在 Line 中定义 BeginTime,然后在 Storyboard 中读取它,这样我就可以做一些更接近的事情:

<UserControl.Resources>
    <!-- Try to read BeginTime from ancestral element -->
    <Storyboard x:Key="myStoryboard" RepeatBehavior="Forever" Duration="0:0:0.8"
                BeginTime="{Binding SomeSource, Property=StoryboardBeginTime}">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
            <LinearDoubleKeyFrame Value="0.5" KeyTime="0%" />
            <LinearDoubleKeyFrame Value="1.0" KeyTime="12.5%" />
            <LinearDoubleKeyFrame Value="0.5" KeyTime="75%" />
            <LinearDoubleKeyFrame Value="0.5" KeyTime="100%" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>
    <!-- Try to send BeginTime to storyboard contained within -->
    <Line Blah="blah" StoryboardBeginTime="0:0:0.1">
        <Line.Triggers>
            <EventTrigger>
                <EventTrigger.Actions>
                    <BeginStoryboard Storyboard="{StaticResource myStoryboard}" />
                </EventTrigger.Actions>
            </EventTrigger>
        </Line.Triggers>
    </Line>

我似乎无法追踪是否有办法在 XAML 中执行此操作,从树上更高的元素向情节提要传递其属性之一的值。这种事情甚至可能吗,还是我应该在代码隐藏中做呢?如果我决定完全调整动画,这并不是现在做的最愉快的事情。

谢谢!

4

1 回答 1

0

似乎这是不可能的 - 虽然它可能与 4.0,但我没有测试这个。您可以使用代码隐藏来执行此操作。

-sa

于 2010-07-23T09:49:41.910 回答