Skip to content

Instantly share code, notes, and snippets.

@kimsk
Created June 17, 2013 03:48
Show Gist options
  • Save kimsk/5794551 to your computer and use it in GitHub Desktop.
Save kimsk/5794551 to your computer and use it in GitHub Desktop.
เราจะเช็คได้อย่างไรว่าผู้ใช้หยุดเปลี่ยนค่า XAML slider แล้ว
<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;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace WinAppThai_SliderEvent
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
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");
// ถ้าเราสร้าง timer ไปแล้ว แสดงว่าผู้ใช้ยังเลื่อนอยู่
// เราจะหยุด timer นั้น
if (_timer != null)
_timer.Stop();
// เราสร้าง timer ใหม่ทุกครั้ง
_timer = new DispatcherTimer();
// เราจะเช็คว่าภายในหนึ่งวินาทีผู้ใช้หยุดเลื่อนแล้วหรือยัง
_timer.Interval = new TimeSpan(0, 0, 1);
// เรากำหนดตรงนี้ว่าถ้าหนึ่งวินาทีผ่านไปแล้ว timer ตัวนี้ยังอยู่ก็แสดงว่าผู้ใช้หยุดแล้ว
// ให้ update textBlock ได้
_timer.Tick += updateTextBlock;
// เริ่ม timer
_timer.Start();
}
void updateTextBlock(object sender, object e)
{
Debug.WriteLine("updateTextBlock");
var i = slider.Value;
textBlock.Text = i.ToString();
// อย่าลืม หยุด และลบ event
_timer.Stop();
_timer.Tick -= updateTextBlock;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment