注:本文参照了http://www.voidcn.com/article/p-sujuobnh-tq.html
一、Clock.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading; //定时器
namespace SilverlightBasic.Clock
{
public partial class Clock : UserControl
{
public Clock()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(enableClock);
}
/**
* 加载执行函数
*/
private void enableClock(object sender,RoutedEventArgs e) {
GetDataTime();//先加载一次
dispatcherTimer mydispatcherTimer = new dispatcherTimer();
mydispatcherTimer.Interval = new TimeSpan(0,1,0);
mydispatcherTimer.Tick += new EventHandler(Timer_Tick);
mydispatcherTimer.Start();
}
/**
* 定时器执行函数
*/
private void Timer_Tick(object sender,EventArgs e) {
GetDataTime();
}
/**
* 处理XAML上文本更新
*/
private void GetDataTime() {
DateTime dataTime = DateTime.Now;
this.txtHour.Text = String.Format("{0:d2}",dataTime.Hour);
this.txtMinute.Text = String.Format("{0:d2}",dataTime.Minute);
this.txtSecond.Text = String.Format("{0:d2}",dataTime.Second);
this.txtMonth.Text = String.Format("{0:d2}",dataTime.Month);
this.txtDay.Text = String.Format("{0:d2}",dataTime.Day);
this.txtWeek.Text = dataTime.DayOfWeek.ToString().Substring(0,3);
}
}
}
二、Clock.xaml
<UserControl x:Class="SilverlightBasic.Clock.Clock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas Width="180" Height="150" Name="Page" Background="#0030628D">
<Rectangle Name="Frame" Width="180" Height="150" stroke="#FF000000" strokeThickness="1"
RadiusX="20" RadiusY="15">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1.1" StartPoint="0.5,-0.1">
<GradientStop Color="#FF259888" Offset="0"/>
<GradientStop Color="#FFC87947" Offset="0.416"/>
<GradientStop Color="#FFC87947" Offset="0.636"/>
<GradientStop Color="#FF259888" Offset="0.981"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Name="Panel" Width="164" Height="134" Fill="#7F91B52C"
stroke="#FFA2AEBF" RadiusX="50" RadiusY="15" Canvas.Left="8"
Canvas.Top="8" strokeThickness="2"/>
<Path Name="Line1" Width="163" Height="1" Fill="#FF100888" Stretch="Fill"
stroke="#FF1B510C" Canvas.Left="8" Canvas.Top="92"
Data="M33.50029,83.29705 l161.89657,83.297051"/>
<Path x:Name="Line2" Width="1" Height="49" Fill="#FF100888" Stretch="Fill"
stroke="#FF1B510C" Canvas.Left="63" Canvas.Top="92"
Data="M81.450752,138.29705 L81.450752,90.29705"/>
<Path x:Name="Line3" Width="1" Height="49" Fill="#FF100888" Stretch="Fill"
stroke="#FF1B510C" Canvas.Left="119" Canvas.Top="92"
Data="M118.30501,164.29698 L118.30501,116.29699"/>
<!-- M D W -->
<TextBlock x:Name="Month" Width="16" Height="19" Canvas.Left="32.5"
Canvas.Top="92" textwrapping="Wrap" Foreground="#FF100888" Text="M"/>
<TextBlock Width="16" Height="19" Canvas.Left="87" Canvas.Top="92"
textwrapping="Wrap" x:Name="Day" Foreground="#FF100888" Text="D"/>
<TextBlock Width="16" Height="19" Canvas.Left="136" Canvas.Top="92"
textwrapping="Wrap" x:Name="Week" Foreground="#FF100888" Text="W"/>
<!-- M D W的值 -->
<TextBlock x:Name="txtMonth" Width="19" Height="19" Canvas.Left="29"
Canvas.Top="111" textwrapping="Wrap" Foreground="#FF100888" Text="12"/>
<TextBlock x:Name="txtDay" Width="20.5" Height="19" Canvas.Left="83.5"
Canvas.Top="111" textwrapping="Wrap" Foreground="#FF100888" Text="31"/>
<TextBlock x:Name="txtWeek" Width="32.5" Height="19" Canvas.Left="130"
Canvas.Top="111" textwrapping="Wrap" Foreground="#FF100888" Text="Sun"/>
<!-- 时间值 -->
<TextBlock x:Name="txtHour" Width="48" Height="48" Canvas.Left="14.5" Canvas.Top="38"
textwrapping="Wrap" FontSize="36" Foreground="#FF100888" Text="23"/>
<TextBlock x:Name="txtMinute" Width="48" Height="48" Canvas.Left="68.5" Canvas.Top="38"
textwrapping="Wrap" FontSize="36" Foreground="#FF100888" Text="59"/>
<TextBlock x:Name="txtSecond" Width="49" Height="48" Canvas.Left="122" Canvas.Top="38"
textwrapping="Wrap" FontSize="36" Foreground="#FF100888" Text="59"/>
<!-- two : -->
<TextBlock x:Name="Colon1" Width="9.5" Height="27" Canvas.Left="62.5" Canvas.Top="48"
textwrapping="Wrap" Foreground="#FF100888" Text=":" FontSize="20"/>
<TextBlock x:Name="Colon2" Width="12" Height="27" Canvas.Left="116.5" Canvas.Top="48"
textwrapping="Wrap" Foreground="#FF100888" Text=":" FontSize="20"/>
<!-- copyright -->
<TextBlock x:Name="copyright" Width="88" Height="16" Canvas.Left="50" textwrapping="Wrap"
FontSize="12" Canvas.Top="22" Foreground="#FF100888" Text="@copyright"/>
</Canvas>
</UserControl>
三、效果图
---------------------------------------------------------------------------------------------------------------------------
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。