Silverlight 圣诞节下雪效果

       记得原来博客园有人写过下雪的程序,今天正好是平安夜大家看看这个下雪效果如何:

Get Microsoft 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
posted @ 2009-12-24 11:38  Gnie  阅读(7070)  评论(35编辑  收藏  举报
Copyright © 2010 Gnie