当我触摸滑块的拇指时会发生什么事件? [英] Which event happens when I touch the thumb of the slider?
问题描述
我正在自定义一个滑块来制作视频搜索栏.我想要的是当我触摸拇指时视频停止.然后我可以拖动拇指并显示与滑块值对应的视频.这是我在 XAML 中的滑块
I'm customize a slider to making seeking bar for video. What I want is that when I touch the thumb the video stops. Then I can drag the thumb and display the video corresponding to the slider's value. This is my Slider in XAML
<Slider Style="{StaticResource SliderStyle1}" Grid.Column="1" x:Name="volumeSlider"
Width="Auto" VerticalAlignment="Center" ValueChanged="volumeSlider_ValueChanged"
DragLeave="volumeSlider_DragLeave" DragEnter="volumeSlider_DragEnter"
DragStarting="volumeSlider_DragStarting"
ManipulationStarted="volumeSlider_ManipulationStarted" ManipulationMode="All"
Tapped="volumeSlider_Tapped"/>
我尝试了很多事件,但是当鼠标指针开始接触拇指时(不是点击或单击),它们都没有发生.有人知道这样的活动吗?
I've try many event but none of them occur when mouse pointer starts touching the thumb (Not tap or click). Does anyone know about such event?
推荐答案
您通常会使用 PointerPressed
事件(参见 docs).然而,这有两个问题.首先,Thumb
已经处理了这种指针交互,因此 PointerPressed
永远不会从 Slider
中冒出.其次,它会在 Slider
的任何位置触发,而不仅仅是拇指.
You would normally use PointerPressed
event (see docs). However, this has two issues. First, the Thumb
already handles such pointer interaction, so PointerPressed
never bubbles out of the Slider
. Secondly, it would be triggered for any location of the Slider
, not only the thumb.
要解决这个问题,您需要处理 Thumb
本身的事件.首先,我们将创建一个搜索可视化树的辅助方法:
To work around this, you will need to handle the event on the Thumb
itself. First, we will create a helper method that searches the visual tree:
public T FindChild<T>(DependencyObject parent)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is T typedChild)
{
return typedChild;
}
var inner = FindChild<T>(child);
if (inner != null)
{
return inner;
}
}
return default;
}
请注意,作为 可视化树扩展.
现在,我们在控件中搜索 Thumb
:
Now, we search for the Thumb
within the control:
var thumb = FindChild<Thumb>(MySlider);
最后,我们附加 PointerPressed
事件.因为它已经被 Slider
控件本身处理了,我们需要使用 AddHandler
,它允许我们观察标记为已处理的事件:
Finally, we attach the PointerPressed
event. Because it is already handled by the Slider
control itself, we need to use AddHandler
, that allows us to observe event that are marked as handled too:
thumb.AddHandler(
UIElement.PointerPressedEvent,
new PointerEventHandler(Thumb_PointerPressed),
true);
然后在处理程序中你可以做任何你需要的事情:
And in the handler you can then do anything you require:
private void Thumb_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// ...
}
这篇关于当我触摸滑块的拇指时会发生什么事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!