您的位置:首页 > 编程语言

演练:使用 Expression Blend 或代码创建 Silverlight 时钟

2009-04-18 22:28 507 查看
Code
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightClock
{
public partial class Page : UserControl
{
public Page()
{
// Required to initialize variables
InitializeComponent();

Loaded += new RoutedEventHandler(SetAndStartClock);

}

private void SetAndStartClock(object sender, EventArgs e)
{
// The current date and time.
System.DateTime currentDate = DateTime.Now;

// Find the appropriate angle (in degrees) for the hour hand
// based on the current time.
double hourangle = (((float)currentDate.Hour) / 12) * 360 + currentDate.Minute / 2;

// The same as for the minute angle.
double minangle = (((float)currentDate.Minute) / 60) * 360;

// The same for the second angle.
double secangle = (((float)currentDate.Second) / 60) * 360;

// Set the beginning of the animation (From property) to the angle
// corresponging to the current time.
hourAnimation.From = hourangle;

// Set the end of the animation (To property)to the angle
// corresponding to the current time PLUS 360 degrees. Thus, the
// animation will end after the clock hand moves around the clock
// once. Note: The RepeatBehavior property of the animation is set
// to "Forever" so the animation will begin again as soon as it completes.
hourAnimation.To = hourangle + 360;

// Same as with the hour animation.
minuteAnimation.From = minangle;
minuteAnimation.To = minangle + 360;

// Same as with the hour animation.
secondAnimation.From = secangle;
secondAnimation.To = secangle + 360;

// Start the storyboard.
clockStoryboard.Begin();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: