首页 > 解决方案 > 当滑块拇指移动绑定到计时器时,鼠标单击 WPF(时间)滑块无效

问题描述

我使用 MediaElement 和 (Time)Slider 来播放和控制视频的播放。我以这个问题的答案 2为基础。

除了拖动功能之外,我还希望将滑块拇指移动到鼠标单击点。当 MediaElement 和 (Time)Slider 暂停时,这可以正常工作,但是当视频正在播放时,鼠标单击无效

这是我的代码

XAML

<MediaElement Source="..." 
              Name="mediaView"
              Height="450" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="UniformToFill" 
              MediaOpened="OnMediaOpened" MediaEnded="OnMediaEnded" MediaFailed="OnMediaFailed" Grid.Row="0" Grid.Column="0"/>
<Grid Name="mediaBar" VerticalAlignment="Bottom" Margin="5,10,5,0" Background="#B2282828"  Grid.Row="0" Grid.Column="0">
    <!-- ... -->
    <Slider Name="timeSlider" Margin="5,5,5,0" 
            Thumb.DragStarted="OnDragStarted" Thumb.DragCompleted="OnDragCompleted" ValueChanged="OnTimeSliderValueChanged" 
            PreviewMouseLeftButtonUp="OnMouseLeftButtonUp" IsMoveToPointEnabled="True"
            MinWidth="200" FlowDirection="LeftToRight"  
            Grid.Column="4" Cursor="ScrollWE" VerticalAlignment="Center"/>
    <!-- ... -->
</Grid>

相关的 c# 部分

private void OnDragStarted(object sender, DragStartedEventArgs args)
{
    isDragging = true;
    ticks.Stop();
}
private void OnDragCompleted(object sender, DragCompletedEventArgs args)
{
    isDragging = false;
    int SliderValue = (int)timeSlider.Value;

    TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
    mediaView.Position = ts;
    if(currentStatus == Status.PLAYING)
        ticks.Start();
}
private void OnMouseLeftButtonUp(object sender, EventArgs ea)
{
    if(!isDragging)
    {
        mediaView.Pause();
        ticks.Stop();

        int SliderValue = (int)timeSlider.Value; // when video is playing this not the point of the mouse click

        // ...
    }
}

我可以理解,timeSlider.Value在播放视频时提供当前时间点而不是鼠标单击位置。

有没有另一种方法来测量鼠标点击的位置并用它来更新滑块值?

或者对于 Mouse-click-while-slider-is-running-situation 的更好解决方案?

标签: wpfslider

解决方案


尝试这个:

private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs ea)
        {
            mediaView.Position = TimeSpan.FromSeconds((int)(mediaView.NaturalDuration.TimeSpan.TotalSeconds * (double)(ea.GetPosition(timeSlider).X / this.Width)));
        }

推荐阅读