public partial class MainView : Window
{
private DispatcherTimer ShowTimer;
public MainView()
{
InitializeComponent();
ShowTime(); //在这里窗体加载的时候不执行文本框赋值,窗体上不会及时的把时间显示出来,而是等待了片刻才显示了出来
ShowTimer = new System.Windows.Threading.DispatcherTimer();
ShowTimer.Tick += new EventHandler(ShowCurTimer);//起个Timer一直获取当前时间
ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
ShowTimer.Start();}
再写一个方法
//ShowTime方法
private void ShowTime()
{
//获得年月日
this.tbDateText.Text = DateTime.Now.ToString("yyyy.MM.dd"); //yyyy/MM/dd
//获得时分秒
this.tbTimeText.Text = DateTime.Now.ToString("HH:mm:ss");
//获取星期几
this.Tt.Text = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
this.Tt.Text += " ";
}
页面写
<TextBlock Height="Auto"
Name="tbDateText"
Foreground="White"
FontWeight="Bold"
FontFamily="Arial"
FontSize="15" Width="83" />
<TextBlock Height="Auto"
x:Name="tbTimeText"
Foreground="White"
FontWeight="Bold"
FontFamily="Calibri"
FontSize="20" Width="109" />
<TextBlock Height="Auto"
x:Name="Tt"
Foreground="White"
FontWeight="Bold"
FontFamily="Calibri"
FontSize="20" Width="109" />
效果
ps
出现报错试试保存或者重新运行程序