- Back to Home »
- Windows store apps C#/XAML »
- Windows store apps development c# xaml Timer example code
Posted by :
Sudhir Chekuri
Saturday, 5 October 2013
Timer Example to increment value on a button for every 1 second
In this example I used a button with content as 0.
When the user click on the button the content on the button will be incremented by 1 for every 1 second.
User can click on the button again to stop the running timer.
XAML code
When the user click on the button the content on the button will be incremented by 1 for every 1 second.
User can click on the button again to stop the running timer.
XAML code
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock FontSize="30">Click on the button to Start/Stop the timer</TextBlock>
<Button x:Name="BtnValue" Content="0" FontSize="50" HorizontalAlignment="Center" Click="BtnValue_Click" />
</Grid>
</Page>
C#.NET code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
int i = 0,j=0;
private void BtnValue_Click(object sender, RoutedEventArgs e)
{
var timer = new DispatcherTimer();
timer.Tick += DispatcherTimerEventHandler;
timer.Interval = new TimeSpan(0, 0, 0, 1);
timer.Start();
}
private void DispatcherTimerEventHandler(object sender, object e)
{
BtnValue.Content = (Convert.ToInt32(BtnValue.Content) + 1).ToString();
}
}
}