c# – wpf故事板死亡
发布时间:2020-12-15 22:56:19 所属栏目:百科 来源:网络整理
导读:C#: public partial class MainWindow : Window{ Storyboard a = new Storyboard(); int i; public MainWindow() { InitializeComponent(); a.Completed += new EventHandler(a_Completed); a.Duration = TimeSpan.FromMilliseconds(10); a.Begin(); } void
C#:
public partial class MainWindow : Window { Storyboard a = new Storyboard(); int i; public MainWindow() { InitializeComponent(); a.Completed += new EventHandler(a_Completed); a.Duration = TimeSpan.FromMilliseconds(10); a.Begin(); } void a_Completed(object sender,EventArgs e) { textblock.Text = (++i).ToString(); a.Begin(); } } XAML: <Window x:Class="Gui.MainWindow" x:Name="control" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="300" Width="300"> <Canvas> <TextBlock Name="textblock"></TextBlock> </Canvas> 这段代码有什么问题? 解决方法
我相信这是因为你的代码在Storyboard的动画时钟和TextBlock的Text DependencyProperty之间没有创建任何关系.如果我不得不猜测我会说故事板何时出来,那么由于污染DependencyProperty(TextBlock.Text是一个DependencyProperty)更新管道,它在某个随机时间.创建如下所示的关联(RunTimeline或RunStoryboard将起作用,但显示查看此内容的替代方法):
public partial class Window1 : Window { Storyboard a = new Storyboard(); StringAnimationUsingKeyFrames timeline = new StringAnimationUsingKeyFrames(); DiscreteStringKeyFrame keyframe = new DiscreteStringKeyFrame(); int i; public Window1() { InitializeComponent(); //RunTimeline(); RunStoryboard(); } private void RunTimeline() { timeline.SetValue(Storyboard.TargetPropertyProperty,new PropertyPath("(TextBlock.Text)")); timeline.Completed += timeline_Completed; timeline.Duration = new Duration(TimeSpan.FromMilliseconds(10)); textblock.BeginAnimation(TextBlock.TextProperty,timeline); } private void RunStoryboard() { timeline.SetValue(Storyboard.TargetPropertyProperty,new PropertyPath("(TextBlock.Text)")); a.Children.Add(timeline); a.Completed += a_Completed; a.Duration = new Duration(TimeSpan.FromMilliseconds(10)); a.Begin(textblock); } void timeline_Completed(object sender,EventArgs e) { textblock.Text = (++i).ToString(); textblock.BeginAnimation(TextBlock.TextProperty,timeline); } void a_Completed(object sender,EventArgs e) { textblock.Text = (++i).ToString(); a.Begin(textblock); } } 这对我来说只要我能让它运行(比以往任何时候都要长10倍). 蒂姆 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- [Swift]LeetCode752. 打开转盘锁 | Open the Lock
- Oracle数据库数据丢失恢复的几种方法总结
- ruby-on-rails-3 – Sidekiq返回延迟方法的值
- 【转载】Oracle 数据库主从切换整理
- 压缩感知重构算法之正则化正交匹配追踪(ROMP)
- 数据库 – MongoDB Auth,可以在shell中进行身份验证,但不能
- vb中利用XmlDocument XmlNode XmlNodeList 解析xml格式的字
- 保留从Ruby中的文件加载YAML的密钥顺序
- 使用wice_grid Ruby On Rails的列中的多个链接
- ruby-on-rails – 专业有用且安全的文件类型列表?