Silverlight顯示日期和時間

來源:互聯網
上載者:User

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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.