开发者

Is there a WPF Typewriter effect?

开发者 https://www.devze.com 2023-01-10 10:46 出处:网络
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)

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

精彩评论

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

关注公众号