我沒有使用綁定.. 我也有類似的問題,使用定時器這個(我的代碼是在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;
}
}
}
第二方向(用戶更新滑塊)
的形式構造函數或類似這樣的東西寫下面一行:
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);
}
}
好吧,它一直調用ValueChanged事件,當用戶單擊滑塊時使用它,因此它移動到媒體文件的位置。滑塊值正在改變,但它不反映在用戶界面:( – HomeMade 2012-04-18 14:05:54
它開始工作,因爲我刪除了另一個邏輯:當用戶單擊滑塊時,它跳轉到那個位置。你能告訴我你是如何解決這個問題的嗎? – HomeMade 2012-04-18 14:32:09
@HomeMade看看我編輯的 – 2012-04-18 14:42:44