Silverlight显示日期和时间

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();

  }
 }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值