private static void TypewriteTextblock(string textToAnimate, TextBlock txt, TimeSpan timeSpan) { var story = new Storyboard { FillBehavior = FillBehavior.HoldEnd }; DiscreteStringKeyFrame discreteStringKeyFrame; var stringAnimationUsingKeyFrames = new StringAnimationUsingKeyFrames { Duration = new Duration(timeSpan) }; var tmp = string.Empty; foreach (var c in textToAnimate) { discreteStringKeyFrame = new 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); }
/// <summary> /// 在指定的文字层绑定控件上进行打字动画 /// </summary> /// <param name="id">层id</param> /// <param name="orgString">原字符串</param> /// <param name="appendString">要追加的字符串</param> /// <param name="msglayBinding">文字层的控件</param> /// <param name="wordTimeSpan">字符之间的打字时间间隔</param> private void TypeWriter(int id, string orgString, string appendString, TextBlock msglayBinding, int wordTimeSpan) { this.HideMessageTria(); Storyboard MsgLayerTypingStory = new Storyboard(); DiscreteStringKeyFrame discreteStringKeyFrame; StringAnimationUsingKeyFrames stringAnimationUsingKeyFrames = new StringAnimationUsingKeyFrames(); Duration aniDuration = new Duration(TimeSpan.FromMilliseconds(wordTimeSpan * appendString.Length)); stringAnimationUsingKeyFrames.Duration = aniDuration; MsgLayerTypingStory.Duration = aniDuration; string tmp = orgString; foreach (char c in appendString) { discreteStringKeyFrame = new DiscreteStringKeyFrame(); discreteStringKeyFrame.KeyTime = KeyTime.Paced; tmp += c; discreteStringKeyFrame.Value = tmp; stringAnimationUsingKeyFrames.KeyFrames.Add(discreteStringKeyFrame); } Storyboard.SetTarget(stringAnimationUsingKeyFrames, msglayBinding); Storyboard.SetTargetProperty(stringAnimationUsingKeyFrames, new PropertyPath(TextBlock.TextProperty)); MsgLayerTypingStory.Children.Add(stringAnimationUsingKeyFrames); MsgLayerTypingStory.Completed += new EventHandler(this.TypeWriterAnimationCompletedCallback); MsgLayerTypingStory.Begin(); this.MsgStoryboardDict[id] = MsgLayerTypingStory; }
private void WorkingAnimation(TextBlock control) { if (_story != null) _story.Stop(this); _story = new Storyboard(); _story.FillBehavior = FillBehavior.HoldEnd; _story.RepeatBehavior = RepeatBehavior.Forever; StringAnimationUsingKeyFrames stringAnimation = new StringAnimationUsingKeyFrames(); stringAnimation.Duration = TimeSpan.FromSeconds(2); stringAnimation.FillBehavior = FillBehavior.Stop; stringAnimation.RepeatBehavior = RepeatBehavior.Forever; Storyboard.SetTargetName(stringAnimation, control.Name); Storyboard.SetTargetProperty(stringAnimation, new PropertyPath(TextBlock.TextProperty)); DiscreteStringKeyFrame kf1 = new DiscreteStringKeyFrame("Working", TimeSpan.FromSeconds(0)); DiscreteStringKeyFrame kf2 = new DiscreteStringKeyFrame(".Working.", TimeSpan.FromSeconds(0.5)); DiscreteStringKeyFrame kf3 = new DiscreteStringKeyFrame("..Working..", TimeSpan.FromSeconds(1)); DiscreteStringKeyFrame kf4 = new DiscreteStringKeyFrame("...Working...", TimeSpan.FromSeconds(1.5)); stringAnimation.KeyFrames.Add(kf1); stringAnimation.KeyFrames.Add(kf2); stringAnimation.KeyFrames.Add(kf3); stringAnimation.KeyFrames.Add(kf4); _story.Children.Add(stringAnimation); DoubleAnimation doubleAnimation = new DoubleAnimation(-2.5, 3.5, TimeSpan.FromSeconds(5.4)); Storyboard.SetTargetName(doubleAnimation, control.Name + "_gs2"); Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(GradientStop.OffsetProperty)); doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; _story.Children.Add(doubleAnimation); _story.Begin(this, true); }
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); }