How to Create the MouseLeftUp event to the slider control in Xamarin Forms

In xamarin forms slider control having only valuechanged event is there when we drag the slider value from 25 to 50 valuechange event is rising 4 or 6 times with random values from 25 to 50, In valueChanged event i am calling REST calls so un necessarily calling the rest calls with random values. So i thought if we use the MouseLeftup event to the slider control it will solve my problem. unfortunately MouseLeftUp event is not there in Slider control in xamarin fomrs

So please help me how to attach a MouseLeftUp event to the slider control in xamarin Forms.

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Mobile platforms don't have mouse events. You could either use a custom renderer to change when the event fires (or fire a new event) or you could use a timer to delay sending the REST call until a certain amount of time has passed since the last value change.

  • DVLNarayanaDVLNarayana USMember
    edited June 2015

    hi Adamkemp,

    first of all thank for your fast response ,

    can you please give the custom renderer for this scenario in andriod?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Sorry, I don't have time to do it for you. This document explains how to make a custom renderer.

  • ylemsoulylemsoul RUMember ✭✭✭
    edited June 2015

    I thought you can use IsFocused property but tried it on Android and WP and have strange results:
    1) in Android IsFocused always False no matter if you dragging the slider or not.
    2) in WP IsFocused changed to True when you dragging it but never unfocused after that. Unfocus() doesn't seem to work.

    Sounds like a bug here but unsure..

    Btw, you can accomplish the second approach what Adam suggested a bit easer and less error prone with Rx:
    `
    public partial class Page1
    {
    public Page1()
    {
    this.InitializeComponent();

        Observable.FromEventPattern<ValueChangedEventArgs>(this.Slider, "ValueChanged")
                    .Select(eventPattern => eventPattern.EventArgs.NewValue)
                    // throttled by half of second
                    .Throttle(TimeSpan.FromMilliseconds(500))
                    .ObserveOn(SynchronizationContext.Current)
                    .Subscribe(this.OnSliderValueChanged);
    }
    
    private void OnSliderValueChanged(double newValue)
    {
        // ..
    }
    

    }
    `

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Is the throttling code the same as what I suggested? Throttling to me usually means you send the first update immediately and then delay the next update. My suggestion was to delay any update until you stop receiving changes for a certain amount of time. That means if you drag only briefly it would send just one update (the last value), whereas a simple throttled approach may either send two (the first and then last) or one (the first only). It's a subtle difference, but I think it may be an important one for this use case.

    How does Rx do throttling? Maybe it does do the same thing?

  • ylemsoulylemsoul RUMember ✭✭✭
    edited June 2015

    Yes, it is do what you say.
    Throttle

Sign In or Register to comment.