1
我正在使用滑塊來顯示正在mediaelement中播放的videofile的進度。我每200ms用一個定時器更新拇指的位置。我還用一個快速前進/快退按鈕改變滑塊的位置,改變媒體元素的位置。 問題是在播放媒體元素期間或使用向前/向後按鈕時拇指的大小會發生變化。它變得更厚,然後再變薄,這是重複的。我該如何解決這個問題?wpf滑塊拇指在移動時改變大小
這裏我上傳,顯示拇指 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;
}
}
我們需要查看相關代碼才能提供幫助。例如,你如何移動拇指?它是標準的Slider控件還是您製作的自定義控件? – 2011-05-30 12:47:59
有沒有人遇到過這個問題? – lupedito 2011-06-04 18:11:40
我可以重現這個問題。即使沒有媒體元素(只需創建一個滑塊並有一個計時器以每N ms遞增一次)。如果遞增完整整數比雙打增加,問題就不那麼明顯了,但它肯定存在。 – 2011-06-14 06:16:13