I am using a slider to control the position of a playing MediaElement. I have implemented the ValueChanged event to synchronize the position and the value of slider. So when I am dragging on the Thumb, the position of MediaElement also changes. However, this creates bad user experience. The ideal case should be like, after user finishes sliding, MediaElement jumps to that final position. So I am looking for events/handlers like AfterSliding.
I have seen solutions like using OnThumbDragCompleted. However, those questions are about WPF and I cannot find such in UWP. Does anyone have a workaround?
Current:
private void ProgressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
MediaPlayer.Position = TimeSpan.FromSeconds(e.NewValue);
LeftTimeTextBlock.Text = MusicDurationConverter.ToTime((int)e.NewValue);
}
Expected:
private void ProgressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
if (!isDragging || slidingFinished) MediaPlayer.Position = TimeSpan.FromSeconds(e.NewValue);
LeftTimeTextBlock.Text = MusicDurationConverter.ToTime((int)e.NewValue);
}
---Update---
I have a timer that ticks every second, but when I tap on the slider (not the thumb) area, the tapped event is not always fired so the thumb will go back and forth between the clicked place and its new position. How can I resolve it so that when user is still holding the thumb, it stays put at the clicked position? I guess the tapped event maybe is not what I should use.
public void Tick()
{
if (ShouldUpdate && !SliderClicked)
{
MediaSlider.Value = MediaControl.Player.PlaybackSession.Position.TotalSeconds;
}
SliderClicked = false;
}
private void MediaSlider_Tapped(object sender, TappedRoutedEventArgs e)
{
Debug.WriteLine("Tapped");
MediaControl.SetPosition(MediaSlider.Value);
SliderClicked = true;
}
private void MediaSlider_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
MediaControl.SetPosition(MediaSlider.Value);
ShouldUpdate = true;
Debug.WriteLine("Completed");
}
private void MediaSlider_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
ShouldUpdate = false;
Debug.WriteLine("Started");
}