我正在为 Windows 10 编写应用程序,需要在 UI 中显示时间。
我是这样制作的
Time.Text = DateTime.Now.ToString("h:mm:ss tt");
但是我需要更新它,关于如何做到这一点有什么建议吗?
最佳答案
在您的 XAML 中试试这个:
<TextBlock x:Name="Time" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
这是你的代码:
public sealed partial class ClockPage : Page
{
DispatcherTimer Timer = new DispatcherTimer();
public ClockPage()
{
InitializeComponent();
DataContext = this;
Timer.Tick += Timer_Tick;
Timer.Interval = new TimeSpan(0, 0, 1);
Timer.Start();
}
private void Timer_Tick(object sender, object e)
{
Time.Text = DateTime.Now.ToString("h:mm:ss tt");
}
}
显然,您需要更改类的名称以匹配您已有的名称,但您明白了。您可能想使用 MVVM 来整理一下,这只是一个简单的示例。
关于c# - 制作时钟 UWP (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38562704/