在 XAML 中,我有<Slider />
. 它有 ValueChanged 事件。每次更改 Value 时都会触发此事件。我需要检测值更改何时结束。LostFocus、PointerReleased 不是正确的事件。我怎样才能检测到这个?
问问题
3032 次
4 回答
10
XAML、WinRT、Windows8.1 和 UWP:
PointerCaptureLost事件应该适用于键盘的鼠标/触摸
KeyUp事件
于 2015-06-16T09:12:28.510 回答
7
您可以创建一个新类并从 Slider 继承。从那里开始,您可以查找 Thumb 控件并监听您想要的事件。
像这样的东西应该工作:
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);
}
}
}
于 2014-11-06T17:01:17.840 回答
2
您可以使用一对布尔值 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();
};
于 2013-08-13T09:02:43.240 回答
1
Slider
我在 Windows8/WinRT 上使用 a 时遇到了类似的问题。
我的问题如下:我正在对事件做出反应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 秒的同时停止,则无论如何都会触发保存操作)。
于 2013-03-12T10:37:12.047 回答