一、需求分析
在工作中遇到的项目中,大部分软件是处于全屏运行状态,这时候就需要在软件的界面上加上日期时间那些,方便用户查看当前时间。
二、功能实现
这里的实现方法也很简单,大致就是自定义一个控件,然后加上文本框显示时间信息,然后后台通过一个定时器,定时去获取系统的时间,然在显示在文本框上。
1、创建自定义控件
给工程新建一个用户控件,然后添加上文本框,这里我添加了三个,分别对应年月日,星期,时间
<UserControl x:Class="ButtonDemo.Control.MyTime"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ButtonDemo.Control"
mc:Ignorable="d"
d:DesignHeight="60" d:DesignWidth="500" Loaded="UserControl_Loaded">
<Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock x:Name="tbDate" Text="2021-09-22" Foreground="White" FontSize="26" FontWeight="Bold"/>
<TextBlock x:Name="tbWeek" Text&