【问题标题】:Storyboard apply to all labels故事板适用于所有标签
【发布时间】:2011-02-02 12:52:35
【问题描述】:

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

<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中创建的,它们是由窗口的构造函数创建的:

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(绑定到标签的名称)。它正在工作......

现在你什么都知道了。

感谢您的帮助:)

【问题讨论】:

  • 我假设“myLabel”在 UniformGrid 中。当我在 XAML 中向统一网格添加两个标签时,您的代码对我有用。你能告诉我们更多关于一切是如何设置的吗?
  • 编辑问题的标题不会关闭它。如果您确实希望关闭该问题,请将其标记为版主并留言。
  • 好的,Aaronaught,我刚刚做到了。

标签: c# wpf animation storyboard


【解决方案1】:

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

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

【讨论】:

  • 感谢斯科特的回答。我知道RepeatBehavior,它只是为了测试。对于我的问题,我第一次像您一样思考,所以我尝试仅将一个标签设置为情节提要的目标,但所有标签都具有动画效果。我想我会尝试直接在 Xaml 中创建 Label,或者使用 XamlWriter 从窗口的构造函数中编写 Xaml。你怎么看?
【解决方案2】:

我找到了解决办法!

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

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

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

感谢您的帮助;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    相关资源
    最近更新 更多