Silverlight 圣诞节下雪效果
记得原来博客园有人写过下雪的程序,今天正好是平安夜大家看看这个下雪效果如何:
Blend Behavior代码如下:
public class FallingSnowBehavior : Behavior<Canvas> { private static Random randomNumber; protected override void OnAttached() { base.OnAttached(); randomNumber = new Random(); this.AssociatedObject.Loaded += new RoutedEventHandler(ApplicationLoaded); } void ApplicationLoaded(object sender, RoutedEventArgs e) { foreach (FrameworkElement element in this.AssociatedObject.Children) { FrameworkElement localCopy = element; double yPosition = Canvas.GetTop(localCopy); double xPosition = Canvas.GetLeft(localCopy); double speed = 2 * randomNumber.NextDouble(); double counter = 0; double radius = 30 * speed * randomNumber.NextDouble(); localCopy.Opacity = 0.2 + randomNumber.NextDouble(); CompositionTarget.Rendering += delegate(object o, EventArgs arg) { counter += Math.PI / (180 * speed); if (yPosition < Application.Current.RootVisual.DesiredSize.Height) { yPosition += 0.2 + speed; } else { yPosition = -localCopy.Height; } Canvas.SetTop(localCopy, yPosition); Canvas.SetLeft(localCopy, xPosition + radius * Math.Cos(counter)); }; } } protected override void OnDetaching() { base.OnDetaching(); } }
源代码下载
参考资料
http://www.kirupa.com/blend_silverlight/falling_snow_silverlight_pg1.htm
作者:李敬然(Gnie)
出处:{GnieTech} (http://www.cnblogs.com/gnielee/)
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。
出处:{GnieTech} (http://www.cnblogs.com/gnielee/)
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。