1

我想将一个小故事板应用于我窗口中的一组标签。我的故事板是这样的:

<Storyboard x:Key="Storyboard1" AutoReverse="True" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label" Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00.1000000" Value="#FFFFFF"/>
        </ColorAnimationUsingKeyFrames>
</Storyboard>

我有一个由它组成的窗口:

<Grid Background="#FF000000">
        <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
            <UniformGrid x:Name="grid" Background="#FF000000" />
        </Viewbox>
</Grid>

当我想开始我的故事板时,我会这样做:

Storyboard.SetTarget( _stb, myLabel );
_stb.Begin();

其中 _std 是由窗口资源加载的故事板。

动画效果很好,但在所有标签上(不仅仅是我想要的)。我尝试通过 SetTargetName 切换 SetTarget,但构造函数在我的窗口中创建了标签,并且当我尝试“SetTargetName”时无法创建名称。

你有什么想法 ?

谢谢 :)

------------ 编辑:我们要求我更具描述性 ----------------------------- --------------------------------------

标签不是直接在 xaml 中创建的,它们是由 window 的构造函数创建的:

public SpellerWindow(IKeyboard keyboard, int colomnNumber, SolidColorBrush background, SolidColorBrush foreground )
{
    InitializeComponent();
    grid.Columns = colomnNumber;
    int i = 0;
    foreach( IKey key in keyboard.Zones.Default.Keys )
    {
        Label lb = new Label();
        lb.Foreground = foreground;
        lb.Name = "label"+(i++).ToString();
        lb.Content = key.ActualKeys[keyboard.CurrentMode].UpLabel;
        lb.HorizontalAlignment = HorizontalAlignment.Center;
        lb.VerticalAlignment = VerticalAlignment.Center;

        Viewbox box = new Viewbox();
        box.Stretch = Stretch.Fill;
        box.Child = lb;
        box.Tag = key;

        grid.Children.Add( box );
    }
}

动画由事件处理程序启动:

void Highlighter_StartAnimation( object sender, HiEventArgs e )
{
      Storyboard stb;
      if( !_anims.TryGetValue( e.Step.Animation.Name, out stb ) )
      {
          stb = (Storyboard)_window.FindResource( e.Step.Animation.Name );
          _anims.Add( e.Step.Animation.Name, stb );
      }

      DoAnimations( _zones[e.Step.Zone], stb );
}

最后,动画由 DoAnimations 启动:

void DoAnimations( List<Label> labels, Storyboard stb )
{
     foreach( Label lb in labels )
     {
         Storyboard.SetTarget( stb, lb );
         stb.Begin();
     }
}

我想突出显示一组标签,但所有标签都在闪烁。我不知道为什么,但我尝试直接在Xaml中创建一个标签,并在情节提要的Xaml中设置一个Storyboard.TargetName(绑定到标签的名称)。它正在工作......

现在你什么都知道了。

谢谢你的帮助:)

4

2 回答 2

0

闪烁是由于情节提要的 RepeatBehavior 设置为永远造成的。这意味着当动画结束时,它会从头开始,重置原始前景色并将其设置为结束颜色。您可能正在寻找的是将 FillBehavior 设置为“HoldEnd”。

所有标签都闪烁的原因是因为您有一个故事板实例并将所有标签连接到它。当故事板开始时,它的所有目标都会被动画化。您需要根据需要添加和删除情节提要目标。

于 2010-03-30T16:32:01.260 回答
0

我找到了解决方案!

我在窗口的构造函数中犯了一个错误:

public SpellerWindow(IKeyboard keyboard, int colomnNumber, SolidColorBrush background, SolidColorBrush foreground )
{
    ....
}

Foreach 键在键盘上创建,我创建了一个新标签,具有给定的背景和前景。当动画改变一个标签的前景时,它会在所有标签上改变,因为所有标签都使用对 SolidColorBrush 的相同引用。

谢谢你的帮助 ;)

于 2010-03-31T08:41:54.687 回答