我正在使用滑块来显示正在媒体元素中播放的视频文件的进度。我每隔200ms用计时器更新拇指的位置。我还使用快进/快退按钮更改滑块的位置,该按钮可更改媒体元素的位置。 问题是在播放媒体元素或使用前进/后退按钮时拇指的大小会发生变化。它再次变厚然后变薄,这是重复的。我该如何解决这个问题?
在这里,我上传了一段显示拇指奇怪行为的视频 http://v7.tinypic.com/player.swf?file=2mw6xhx&s=7
这是代码:
xaml中滑块
<Slider Name="sliderPosition"
Width="220"
Margin="2"
ValueChanged="sliderPosition_ValueChanged"
Thumb.DragStarted="sliderPosition_DragStarted"
Thumb.DragCompleted="sliderPosition_DragCompleted" />
代码
//initialization
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(200);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
private void OnMediaOpened(object sender, RoutedEventArgs e)
{
if (MediaElem.NaturalDuration.HasTimeSpan)
{
TimeSpan ts = TimeSpan.FromMilliseconds(MediaElem.NaturalDuration.TimeSpan.TotalMilliseconds * 0.95);
sliderPosition.Maximum = ts.TotalSeconds;
sliderPosition.SmallChange = 1;
sliderPosition.LargeChange = Math.Min(10, ts.Seconds / 10);
tbPosition.Text = String.Format("00:00:00");
tbDuration.Text = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
}
}
private void MoveForward(int millisec)
{
if ((MediaElem.Position.TotalMilliseconds + millisec) > (MediaElem.NaturalDuration.TimeSpan.TotalMilliseconds * 0.95))
{
MediaElem.Position = TimeSpan.FromMilliseconds(MediaElem.NaturalDuration.TimeSpan.TotalMilliseconds * 0.95);
}
else
{
MediaElem.Position = MediaElem.Position + TimeSpan.FromMilliseconds(millisec);
sliderPosition.Value = MediaElem.Position.TotalSeconds;
}
}
private void MoveBackward(int millisec)
{
if ((MediaElem.Position - TimeSpan.FromMilliseconds(millisec)) < TimeSpan.FromMilliseconds(0))
{
MediaElem.Position = TimeSpan.FromMilliseconds(0);
}
else
{
MediaElem.Position = MediaElem.Position - TimeSpan.FromMilliseconds(millisec);
sliderPosition.Value = MediaElem.Position.TotalSeconds;
}
}
private void sliderPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
TimeSpan ts = TimeSpan.FromSeconds(e.NewValue);
tbPosition.Text = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds);
MediaElem.Position = ts;
}
private void sliderPosition_DragStarted(object sender, DragStartedEventArgs e)
{
isDragging = true;
}
private void sliderPosition_DragCompleted(object sender, DragCompletedEventArgs e)
{
isDragging = false;
MediaElem.Position = TimeSpan.FromSeconds(sliderPosition.Value);
}
void timer_Tick(object sender, EventArgs e)
{
if (!isDragging && isPlaying)
{
sliderPosition.Value = MediaElem.Position.TotalSeconds;
}
}
答案 0 :(得分:0)
拇指的绘制方式肯定存在问题。在Windows 7/64下运行,滑块拇指移动可怕。拇指是使用花哨的航空模式主题模块绘制的,开箱即用;所以在预感中,我启动了Blend并放入了“SimpleSlider”,这只是一个完全自定义和自包含的样式和模板的常规。一旦我这样做,拇指开始移动非常顺利。
您可以尝试使用SimpleSlider样式,也可以从头开始绘制自己的模板。