Skip to content

Instantly share code, notes, and snippets.

@kimsk
Created June 17, 2013 04:08
Show Gist options
  • Save kimsk/5794602 to your computer and use it in GitHub Desktop.
Save kimsk/5794602 to your computer and use it in GitHub Desktop.
How to detect when a user completes changing WinRT XAML Slider value?
<Page
x:Class="WinAppThai_SliderEvent.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinAppThai_SliderEvent"
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}">
<StackPanel>
<Slider x:Name="slider" ValueChanged="Slider_ValueChanged"/>
<TextBlock x:Name="textBlock"/>
</StackPanel>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
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;
namespace WinAppThai_SliderEvent
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private DispatcherTimer _timer = null;
private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
Debug.WriteLine("Slider_ValueChanged");
// If we have a timer and we are in this event handler, a user is still interact with the slider
// we stop the timer
if (_timer != null)
_timer.Stop();
// we always create a new instance of DispatcherTimer
_timer = new DispatcherTimer();
// if one second passes, that means our user has stopped interacting with the slider
// we do real event
_timer.Interval = new TimeSpan(0, 0, 1);
_timer.Tick += updateTextBlock;
_timer.Start();
}
void updateTextBlock(object sender, object e)
{
Debug.WriteLine("updateTextBlock");
var i = slider.Value;
textBlock.Text = i.ToString();
// Don't forget to stop the timer :-)
_timer.Stop();
_timer.Tick -= updateTextBlock;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment