wpf - 当滑块拇指移动绑定到计时器时,鼠标单击 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 的更好解决方案?
解决方案
尝试这个:
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs ea)
{
mediaView.Position = TimeSpan.FromSeconds((int)(mediaView.NaturalDuration.TimeSpan.TotalSeconds * (double)(ea.GetPosition(timeSlider).X / this.Width)));
}
推荐阅读
- reactjs - 屏幕唤醒,空闲计时器
- npm - 如何将模拟器更新到 firebase-tools-instant-win.exe 中的节点 v14?
- python - 为什么我的程序崩溃并反复播放声音?(Python tkinter 计时器)
- java - 无法使用 JPA 找到数据在 MySQL 中的持久化位置
- pandas - 将字符串转换为标识正确年份的确切日期
- flutter - 如果链接不起作用,则颤动媒体 url 重定向到另一个 url
- javascript - 如何在jquery中获取校验值?
- javascript - 直接在特定元素上呈现 html 页面而不滚动(Django/JavaScript)
- c# - Blazor 和 Microsoft.AspNetCore.Identity 需要电子邮件
- python - 将单元格中的 Excel 公式转换为 Python 脚本中的函数