MainPage.xaml文件代码:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SLTimer.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<TextBlock Height="21" HorizontalAlignment="Left" Margin="127,75,0,0" VerticalAlignment="Top" Width="57" Text="当前时间:" TextWrapping="Wrap" FontSize="12" Foreground="#FF315DAB"/>
<TextBlock x:Name="tbTime" Height="21" Margin="188,75,94,0" VerticalAlignment="Top" TextWrapping="Wrap"/>
</Grid>
</UserControl>
MainPage.xaml.cs文件代码:
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;
using System.Windows.Threading;
namespace SLTimer
{
public partial class MainPage : UserControl
{
public MainPage()
{
// 为初始化变量所必需
InitializeComponent();
}
private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// TODO: Add event handler implementation here.
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0,0,1);//时间间隔为1s
timer.Tick +=new System.EventHandler(timer_Tick);
timer.Start();
}
private void timer_Tick(object sender, System.EventArgs e)
{
// TODO: Add event handler implementation here.
//首先获取是星期几
string week = DateTime.Now.DayOfWeek.ToString();
switch(week)
{
case "Monday":
week = "星期一";
break;
case "Tuesday":
week = "星期二";
break;
case "Wednesday":
week = "星期三";
break;
case "Thurday":
week = "星期四";
break;
case "Friday":
week = "星期五";
break;
case "Saturday":
week = "星期六";
break;
case "Sunday":
week = "星期日";
break;
}
this.tbTime.Text = DateTime.Now.Year + " 年 " + DateTime.Now.Month + " 月 " + DateTime.Now.Day + " 日 "+ week +" "+ DateTime.Now.ToLongTimeString();
}
}
}