nsds
nsds

Reputation: 981

DragStarted event is not getting in UWP Slider - c#

I want to change the video position according to how much I drag the slider. So that I want to use DragStarted and DragCompleted events. But it shows "The local property 'Dragstarted' can only be applied to types that are derived from 'Thumb'". How can I include it in my app?

 <Slider Name="timelineSlider" Thumb.DragCompleted= "timelineSlider_DragCompleted" DragStarting="timelineSlider_DragStarting" DragOver="timelineSlider_DragOver" Width="300" />

Also found that there is DragStarting and DragOver events. Does it have the same functionality of Dragstarted and DragCompleted events? Unfortuantely DragStarting and DragOver events are not firing when I drag the slider.

Upvotes: 3

Views: 1346

Answers (3)

Kamil
Kamil

Reputation: 11

PointerCaptureLost works well in UWP App.

Examle XAML:

<Slider x:Name="SeekSlider" PointerCaptureLost="SeekSlider_PointerCaptureLost"/>

Example C#:

private void SeekSlider_PointerCaptureLost(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
    Slider slider = sender as Slider;
    if (slider != null)
    {
        //mediaPlayer.Position = (float)slider.Value;
    }
}

It works for me.

Upvotes: 1

Breeze Liu - MSFT
Breeze Liu - MSFT

Reputation: 3808

You can try the ManipulationStarted and ManipulationCompleted event of the Slider.

 <Slider ManipulationCompleted="Slider_ManipulationCompleted" ManipulationMode="All" ManipulationStarted="Slider_ManipulationStarted"/>

You must set the ManipulationMode to a value other than System or None if you want to handle manipulation events such as ManipulationStarted from UI elements in your app code.

private void Slider_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
    var slide= sender as Slider;
    Debug.WriteLine(slide.GetType().ToString());
    Debug.WriteLine(slide.Value+ ">>Slider_ManipulationCompleted");
}

private void Slider_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
    var slide = sender as Slider;
    Debug.WriteLine(slide.GetType().ToString());
    Debug.WriteLine(slide.Value+ "Slider_ManipulationStarted");
}

Upvotes: 3

Martin Zikmund
Martin Zikmund

Reputation: 39082

You can more easily use the ValueChanged event to see the changes in the position of the Slider:

<Slider x:Name="Slider" ValueChanged="Slider_OnValueChanged" />

You can use the e.NewValue to get the new value of the slider and the e.OldValue to get the previous position.

private void Slider_OnValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    Debug.WriteLine($"Slider moved from {e.OldValue} to {e.NewValue}");
}

Upvotes: 0

Related Questions