尝试将滑块的最大值绑定到媒体元素的持续时间并将滑块的当前值绑定到媒体元素的位置,但由于某些原因它没有。
我希望滑块在视频播放时移动拇指。
<Slider x:Name="videoSlider" Value="{Binding ElementName=mp3MediaElement, Path=Position}"
ValueChanged="videoSlider_ValueChanged" IsMoveToPointEnabled="True"
Maximum="{Binding ElementName=mp3MediaElement, Path=NaturalDuration}"
AllowDrop="True" DataContext="{Binding ElementName=mp3MediaElement}" />
我没有使用绑定..
我遇到了类似的问题,并为此使用了计时器(我的代码在 Silverlight 中,它在 WPF 上应该是相同的):
第一个方向(影片更新滑块)
private TimeSpan TotalTime;
private void MyMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
TotalTime = MyMediaElement.NaturalDuration.TimeSpan;
// Create a timer that will update the counters and the time slider
timerVideoTime = new DispatcherTimer();
timerVideoTime.Interval = TimeSpan.FromSeconds(1);
timerVideoTime.Tick += new EventHandler(timer_Tick);
timerVideoTime.Start();
}
void timer_Tick(object sender, EventArgs e)
{
// Check if the movie finished calculate it's total time
if (MyMediaElement.NaturalDuration.TimeSpan.TotalSeconds > 0)
{
if (TotalTime.TotalSeconds > 0)
{
// Updating time slider
timeSlider.Value = MyMediaElement.Position.TotalSeconds /
TotalTime.TotalSeconds;
}
}
}
第二方向(用户更新滑块)
在 ctor 或类似的东西上写下以下行:
timeSlider.AddHandler(MouseLeftButtonUpEvent,
new MouseButtonEventHandler(timeSlider_MouseLeftButtonUp),
true);
事件处理程序是:
private void timeSlider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (TotalTime.TotalSeconds > 0)
{
MyMediaElement.Position = TimeSpan.FromSeconds(timeSlider.Value * TotalTime.TotalSeconds);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)