I need a event for the slider it should fire only if user ends touching the control

3.4k views Asked by At

My Requriment : I need a event for slider it should fire only if user ends touching the control

Custom Control :

 public class ExtendedSlider : Slider
    {
        public event EventHandler StopedDraging;
        public void OnStopedDrag()
        {
            if (StopedDraging != null)
            {
                StopedDraging(this,EventArgs.Empty);
            }
        }
    }

UI :

      <ListView.ItemTemplate >
                  <Label Text="{Binding luminaireLevel, StringFormat='{0:F0}%'}" />
           <PCAControls:ExtendedSlider Maximum="100" Minimum="25" 
                                            Value="{Binding luminaireLevel, Mode=TwoWay}"
                                            LuminaireID="{Binding id}"
                                            StopedDraging="ExtendedSlider_StopedDraging"
                                            />                 
      </ListView.ItemTemplate>

Renderer :

class ExtendedSliderRenderer : SliderRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            var slider = (PCA.CustomControls.ExtendedSlider)e.NewElement;
            Control.Max = (int)(slider.Maximum - slider.Minimum);
            Control.Progress = (int)(slider.Value - slider.Minimum);
            Control.StopTrackingTouch += Control_StopTrackingTouch;
        }
    }

    void Control_StopTrackingTouch(object sender, SeekBar.StopTrackingTouchEventArgs e)
    {
        var slider = (PCA.CustomControls.ExtendedSlider)Element;
        slider.Value = Control.Progress + slider.Minimum; 
        slider.OnStopedDrag();
    }
} 

Problem is : I achieved what i expected, but user stop draging the slider or tap between the slider , luminaireLevel (vewmodel property) is updating but the slider always showing the full progess

1

There are 1 answers

0
MyKuLLSKI On

When your renderer changes the value of the iOS control the "binding" isn't Two=Way in that respect. To achieve what you want you need to bind the Xamarin.Forms Slider to value in your viewmodel then in your renderer you change the value in your viewmodel.

If you bind all your properties (min, max, value, progress) itll be easier