SilverLight学习blog

http://www.experience10.com/archive.aspx

http://demos.telerik.com/silverlight/#Home 

简化Animation语法

 

如果你不想在new一个DoubleAnimation()后,做大量的From,To,Duration的操作的话,可以试试C#所支持的简写对象的方式,而且在VS的智能感知中就能原生的支持
简写前
DoubleAnimation opacityAnimation = new DoubleAnimation()
opacityAnimation.From = 0;
opacityAnimation.To = 1;
opacityAnimation.Duration = duration;


简写后
DoubleAnimation opacityAnimation = new DoubleAnimation() { From = 0, To = 1, Duration = duration};
====================================================================
DispatcherTimer vs Storyboard
Silverlight中的Mask动画
真正的Loaded事件
 

真正的Loaded事件

 
做了一些Silverlight开发应该知道,在Loaded事件之后的那个handler之中,画面并没有实际的被layout出来,在Loaded的时候是获得不了ActualWidth或其它相关值的,而只有在Layoutupdated事件中才能获得,但Layoutupdated事件是经常会运行,容易消耗资源,因此有必要找到一个方法,在画面正确渲染出来后,也就是被layouted时,来进行一些操作,之前在WPF中采用过这样一个方法,在这几天也在想办法在Silverlight找寻类似的方法,结论代码如下:
//构造中加Loaded事件
Loaded+=new RoutedEventHandler(Page_Loaded);
//Loaded Event handler
void Page_Loaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Loaded Event:"+this.ActualWidth);
Thread thread = new Thread(new ThreadStart(() =>
{
this.Dispatcher.BeginInvoke(new Action(() =>
{
Debug.WriteLine("Thread Start After Loaded Event:" + ActualWidth);
}));
}));
thread.Start();
}


输出结果:
Loaded Event:0
Thread Start After Loaded Event:400


在我的项目中已经能很准确的获得ActualWidth,未经更复杂的layout情况下的证实,但我想应该是目前我能用到的唯一的解决方案,在构造中调用线程也是一样的效果,简单写一下,可以像有一个UILoaded事件一样:

public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
(new Thread(new ThreadStart(() => { Dispatcher.BeginInvoke(UILoaded); }))).Start();
}
void UILoaded()
{
Debug.WriteLine("UILoaded:" + ActualWidth);
}
}
posted @ 2008-11-26 17:59  IamV  阅读(354)  评论(0编辑  收藏  举报