在XAML中,我有<Slider />
。它有ValueChanged事件。每次更改Value时都会触发此事件。我需要检测值变化何时结束。 LostFocus,PointerReleased不是正确的事件。我怎么能发现这个?
答案 0 :(得分:8)
XAML, WinRT ,Windows8.1和UWP:
PointerCaptureLost 事件应该适用于鼠标/触控
键盘 KeyUp 事件
答案 1 :(得分:7)
您可以创建一个新类并从Slider继承。从那以后,你可以寻找拇指控制&amp;听你想要的事件。
这样的事情应该有效:
public class SliderValueChangeCompletedEventArgs : RoutedEventArgs
{
private readonly double _value;
public double Value { get { return _value; } }
public SliderValueChangeCompletedEventArgs(double value)
{
_value = value;
}
}
public delegate void SlideValueChangeCompletedEventHandler(object sender, SliderValueChangeCompletedEventArgs args);
public class ExtendedSlider : Slider
{
public event SlideValueChangeCompletedEventHandler ValueChangeCompleted;
private bool _dragging = false;
protected void OnValueChangeCompleted(double value)
{
if (ValueChangeCompleted != null)
{
ValueChangeCompleted(this, new SliderValueChangeCompletedEventArgs(value) );
}
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
var thumb = base.GetTemplateChild("HorizontalThumb") as Thumb;
if (thumb != null)
{
thumb.DragStarted += ThumbOnDragStarted;
thumb.DragCompleted += ThumbOnDragCompleted;
}
thumb = base.GetTemplateChild("VerticalThumb") as Thumb;
if (thumb != null)
{
thumb.DragStarted += ThumbOnDragStarted;
thumb.DragCompleted += ThumbOnDragCompleted;
}
}
private void ThumbOnDragCompleted(object sender, DragCompletedEventArgs e)
{
_dragging = false;
OnValueChangeCompleted(this.Value);
}
private void ThumbOnDragStarted(object sender, DragStartedEventArgs e)
{
_dragging = true;
}
protected override void OnValueChanged(double oldValue, double newValue)
{
base.OnValueChanged(oldValue, newValue);
if (!_dragging)
{
OnValueChangeCompleted(newValue);
}
}
}
答案 2 :(得分:1)
我在Windows8 / WinRT上使用Slider
时出现了类似的问题。
我的问题如下:我对ValueChanged
事件作出反应并在每次触发后执行持久操作(异步写入文件)。因此遇到并发编辑异常。为了避免这种情况,我使用了DispatcherTimer
。
//Class member
private DispatcherTimer myDispatcherTimer = null;
private void OnSliderValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
//I update my UI right away
...
//If the dispatcher is already created, stop it
if (myDispatcherTimer!= null)
myDispatcherTimer.Stop();
//Overwrite the DispatcherTimer and thus reset the countdown
myDispatcherTimer= new DispatcherTimer();
myDispatcherTimer.Tick += (sender, o) => DoSomethingAsync();
myDispatcherTimer.Interval = new TimeSpan(0,0,2);
myDispatcherTimer.Start();
}
private async void DoSomethingAsync()
{
await DoThatLongSaveOperation();
}
你不能直接检测到最终值是什么,但你至少可以延迟操作,直到两次更新之间有一个长时间停顿(例如,在我的情况下,如果用户拖动滑块并在保持拖动2时停止)秒,保存操作无论如何都会被激活。)
答案 3 :(得分:1)
你可以使用一对bool值isValueChanged和(如果可能的话,在不操纵指针的情况下改变值) )isPressed;
private void Slider_ValueChanged(object s, RangeBaseValueChangedEventArgs e) {
if (!isPressed) {
AcceptChanges();
} else {
isValueChanged = true;
}
}
初始化代码:
Window.Current.CoreWindow.PointerPressed += (e, a) => { isPressed = true; };
Window.Current.CoreWindow.PointerReleased += (e, a) => {
isPressed = false;
if (isValueChanged) AcceptChanges();
};