views:

50

answers:

2

Is there any equivalent to Flash's Typewriter effects in WPF?

Thanks in advance! MemphiZ

A: 

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.

CommanderZ
Thanks for the tip! With that I was able to do what I posted as answer.
MemphiZ
Fading in: I guess that's most likely not possible with strings, you would have to take completely new approach. Make a WrapPanel and add a TextBlock with letter to it every N milliseconds (you can use DispatcherTimer). Then you can use DoubleAnimation class to simply make every letter fade in over a time period (animate the OpacityProperty). This is however not going to be very effective on longer strings!
CommanderZ
A: 

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?

MemphiZ
For a follow-up question best start a new separate question. More people would see it and try to answer if you post it asa question on it's own.
sth