Slider used in Xamarin.froms to control value

948 Views Asked by At

I'm adding a slider into my xamarin app, but im using view models to capture values. How can I move the slider on the GUI and show the value incrementing up and down and then capture its value?

or is there a way I can include the "MySlider_ValueChanged" event and apply it to the view model page rather than the content page code behind file?

1

There are 1 best solutions below

0
On

is there a way I can include the "MySlider_ValueChanged" event and apply it to the view model page rather than the content page code behind file?

You have to find a BindableProperty which could be bound inside view model(ICommand) ,it should do the same work as the event ValueChanged .

For exmaple

We could handle Tapped event of TapGestureRecognizer in page code behind , we also could create new ICommand inside view model , and bind it with Command of TapGestureRecognizer .

However, Slider does not have command whose function is detecting the value changed , there is only DragStartedCommand and DragCompletedCommand , so the only solution is to trigger your method inside the setter method of Value .

Xaml
<Slider  Value="{Binding CurrentProgress}" />
View Model
public double CurrentProgress
{
    get { return currentProgress; }
    set
    {
        currentProgress = value;
        NotifyPropertyChanged("CurrentProgress");
        YourMethod(value);
    }
}

Refer https://stackoverflow.com/a/25141043/8187800.