1. 程式人生 > >WPF打字機效果

WPF打字機效果

each ani eat code tor property one ret block

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            TypewriteTextblock("我愛北京天安門,我愛祖國",this.TextBlockTest,new TimeSpan(1000 * 1000 * 10*10));

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

WPF打字機效果