开发者

Is there a WPF Typewriter effect?

Is there any equivalent to Flash's Typewriter effects开发者_JS百科 in WPF?


OK I made it work!

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);
    }

But is there a way to have the characters fade in?


By typewriter effects you mean the string being displayed letter by letter?

You can achieve similar effect with StringAnimationUsingKeyframes object, however, you would have to enter every string value manually.

To create this effect automatically, you would have to write your own animation object, most likely one based on StringAnimationBase class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜