我有一个基本的微调器类型控件,我已经掀起了它,它运行得相当好,但整个过程中都有大量的重复代码。除了两个属性之外,它本质上是相同的元素,其中一个属性是Storyboard的BeginTime - 每一行只是无休止地重复它的动画,但每一行在前一个之后开始.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的唯一变化是Storyboard的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中执行此操作,将故事板中的一个属性的值从树上方的元素传递给它。这种类型的东西是否可能,或者我应该在代码隐藏中代替它?如果我决定完全调整动画,那么现在做的并不是最令人愉快的事情。
谢谢!
答案 0 :(得分:0)
似乎这是不可能的 - 虽然可能有4.0,但我没有测试这个。您可以使用代码隐藏来完成此操作。
-sa