user5677145
user5677145

Reputation:

Make Clock UWP (C#)

I'm writing application for windows 10 and need to display Time inside the UI.

I made the display like this

 Time.Text = DateTime.Now.ToString("h:mm:ss tt");

But I need to update it, any advice on how I can do this?

Upvotes: 3

Views: 6844

Answers (3)

Frauke
Frauke

Reputation: 1582

Try this inside your XAML:

    <TextBlock x:Name="Time" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>

and this as your code:

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

Obviously you'll need to change the name of the class to match what you've got, but you get the idea. You may want to tidy things up by using MVVM, this is just a bare-bones example.

Upvotes: 12

Mafii
Mafii

Reputation: 7455

You can simply add a Timer to your class:

private Timer timer;

Create a handler for the Elapsed event of the timer:

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    Time.Text = DateTime.Now.ToString("h:mm:ss tt");
}

And then in the constructor of your window, you can add this:

timer = new Timer(1000); // let the timer tick every 1000 ms = every second
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true; // Enable it

Upvotes: 2

Francesca Aldrovandi
Francesca Aldrovandi

Reputation: 311

<Window x:Class="StkOverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:StkOverflow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBlock Text="{Binding Time, StringFormat='{}{0: h:mm:ss tt}'}"></TextBlock>
</Grid>

using System;
using System.Windows;

namespace StkOverflow
{
public partial class MainWindow
{
    System.Windows.Threading.DispatcherTimer Timer = new System.Windows.Threading.DispatcherTimer();

    public DateTime Time
    {
        get { return (DateTime)GetValue(TimeProperty); }
        set { SetValue(TimeProperty, value); }
    }

    public static readonly DependencyProperty TimeProperty =
        DependencyProperty.Register("Time", typeof(DateTime), typeof(MainWindow), new PropertyMetadata(DateTime.Now));

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        Timer.Tick += new EventHandler(Timer_Click);
        Timer.Interval = new TimeSpan(0, 0, 1);
        Timer.Start();
    }

    private void Timer_Click(object sender, EventArgs e)
    {
        Time = DateTime.Now;
    }
}
}

Upvotes: 8

Related Questions