Reputation: 11
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?
Upvotes: 1
Views: 987
Reputation: 14475
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
.
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.
Upvotes: 1