The only way to show current time updating regularly I found is to use timer. Of course, I can implement INotifyPropertyChanged
and some special property to be
Here is a little code sample that works without a timer:
public DateTime CurrentTime
{
get => DateTime.Now;
}
public CurrentViewModelTime(object sender, RoutedEventArgs e)
{
_ = Update(); // calling an async function we do not want to await
}
private async Task Update()
{
while (true)
{
await Task.Delay(100);
OnPropertyChanged(nameof(CurrentTime)));
}
}
Of course, this Update() function never returns, but it does its loop on a threadpool thread and does not even block any thread for long.
You can perfectly also implement this in the window directly without a viewmodel.