【问题标题】:Mouse click on WPF (Time)Slider has no effect when slider thumb is moving bound to a timer当滑块拇指移动绑定到计时器时,鼠标单击 WPF(时间)滑块无效
【发布时间】:2018-11-08 13:19:36
【问题描述】:

我使用 MediaElement 和 (Time)Slider 来播放和控制视频的播放。 我使用answer 2 to this question 作为基础。

除了拖动功能之外,我还希望将滑块拇指移动到鼠标单击点。 这在 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在播放视频时传递的是当前时间点而不是鼠标点击位置。

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

或者对于鼠标单击而滑块正在运行的情况的更好解决方案?

【问题讨论】:

    标签: wpf slider


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • this.Width 或 timeSlider.Width 不起作用,因为 timeSlider 位于 Grid ColumnDefinition Width="200*" 中,最后 timeSlider.ActualWidth 成功了。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多