1
我已经有了一个基本的微调控件类型控件,我已经鞭打过了,它工作得很好,但其中有大量的重复代码。除了两个属性之外,它本质上是相同的元素,其中一个属性是Storyboard的BeginTime - 每一行只是无休止地重复它的动画,但每行都在前一个动画之后以秒为单位开始。小波浪效应。WPF - 从父元素分配故事板开始时间
下面的代码的总体思路:
<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的方式,我可以定义的BeginTime的线,然后从它在故事板读,所以我可以做一些更接近:
<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中执行此操作的方法,可将故事板的某个属性值从树上的某个元素传递给故事板。这种类型的事情甚至是可能的,还是应该在代码隐藏方面做呢?如果我决定完全调整动画,现在做的并不是最令人愉快的事情。
谢谢!