记得原来博客园有人写过下雪的程序,今天正好是平安夜大家看看这个下雪效果如何:
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博客园博客,原文链接:http://www.cnblogs.com/gnielee/archive/2009/12/24/silverlight-snow.html,如需转载请自行联系原作者