2010-08-07 88 views

回答

6

好吧,我做到了!

private void TypewriteTextblock(string textToAnimate, TextBlock txt, TimeSpan timeSpan) 
    { 
     Storyboard story = new Storyboard(); 
     story.FillBehavior = FillBehavior.HoldEnd; 
     story.RepeatBehavior = RepeatBehavior.Forever; 

     DiscreteStringKeyFrame discreteStringKeyFrame; 
     StringAnimationUsingKeyFrames stringAnimationUsingKeyFrames = new StringAnimationUsingKeyFrames(); 
     stringAnimationUsingKeyFrames.Duration = new Duration(timeSpan); 

     string tmp = string.Empty; 
     foreach(char c in textToAnimate) 
     { 
      discreteStringKeyFrame = new DiscreteStringKeyFrame(); 
      discreteStringKeyFrame.KeyTime = KeyTime.Paced; 
      tmp += c; 
      discreteStringKeyFrame.Value = tmp; 
      stringAnimationUsingKeyFrames.KeyFrames.Add(discreteStringKeyFrame); 
     } 
     Storyboard.SetTargetName(stringAnimationUsingKeyFrames, txt.Name); 
     Storyboard.SetTargetProperty(stringAnimationUsingKeyFrames, new PropertyPath(TextBlock.TextProperty)); 
     story.Children.Add(stringAnimationUsingKeyFrames); 

     story.Begin(txt); 
    } 

但是有没有办法让人物淡入?

+0

对于后续问题,最好开始一个新的单独问题。更多的人会看到它,并试图回答,如果你发布它自己的问题作为 。 – sth 2010-08-15 01:43:59

0

通过打字机效果你的意思是字符串显示的字母?

您可以使用StringAnimationUsingKeyframes对象实现类似效果,但是,您必须手动输入每个字符串值。

要自动创建此效果,您必须编写自己的动画对象,最有可能是基于StringAnimationBase类的动画对象。

+0

感谢您的提示!有了这个,我可以做我发布的答案。 – MemphiZ 2010-08-07 19:59:53

+0

淡入淡出:我想这很可能不可能用字符串,你必须采取全新的方法。制作一个WrapPanel并每隔N毫秒添加一个带有字母的TextBlock(可以使用DispatcherTimer)。然后,您可以使用DoubleAnimation类简化每个字母在一段时间内的淡入淡出效果(为OpacityProperty设置动画效果)。然而,这对于更长的琴弦不会很有效! – 2010-08-07 20:18:36