0

I am trying to put in function an IOS custom renderer for Slider control. Due to wrapping class SlideriOS the event are not firing and I need DragCompleted event. Somebody has an idea how to trigger/route the event to the control?

[assembly: ExportRenderer(typeof(CustomGradientSlider), typeof(CustomGradientSliderRenderer))]
namespace HealMate.iOS.Renderers
{
    public class CustomGradientSliderRenderer : SliderRenderer
    {
        public CGColor StartColor { get; set; }
        public CGColor CenterColor { get; set; }
        public CGColor EndColor { get; set; }
        private UIImage GradImage { get; set; }
        public string SelectionColor1 { get; set; }
        public string SelectionColor2 { get; set; }

    protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
    {

        if (Control == null)
        {
            var customSlider = e.NewElement as CustomGradientSlider;
            StartColor = customSlider.StartColor.ToCGColor();
            CenterColor = customSlider.CenterColor.ToCGColor();
            EndColor = customSlider.EndColor.ToCGColor();


              var slider = new SlideriOS
            {
                Continuous = true,

                Height = (nfloat)customSlider.HeightRequest
            };

            SetNativeControl(slider);
            Control.ValueChanged += OnControlValueChanged;

        }

        base.OnElementChanged(e);
    }

...

   public class SlideriOS : UISlider
    {
        public nfloat Height { get; set; }

        public override CGRect TrackRectForBounds(CGRect forBounds)
        {
            var rect = base.TrackRectForBounds(forBounds);
            return new CGRect(rect.X, rect.Y, rect.Width, Height);
        }
        }
}

How can I fire the DragCompleted event for the slider control ? Thanks!

1 Answers1

1

There are some Events can be invoked in UISlider :

  • TouchDragExit : Raised on TouchDragExit events.
  • TouchUpInside : Raised on TouchUpInside events.
  • TouchUpOutside : Raised on TouchUpOutside events.

You can invoke one of them to have a try as follow :

Control.TouchDragExit += Slider_TouchDragExit;

private void Slider_TouchDragExit(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

===================================Update====================================

After testing in local project , above three methods only TouchUpInside works ,and not finding the reason why other methods not working in iOS renderer . I also test renderer methods in Android, it works . It is a strange phenomenon.

However , I found a workaround to invoke methods something as drag ended function.

[assembly: ExportRenderer(typeof(CustomSlider), typeof(CustomSliderRenderer))]
namespace App8.iOS
{
    public class CustomSliderRenderer : SliderRenderer
    {

    protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
    {
        base.OnElementChanged(e);

        if (null != Control)
        {
            CustomSlider customSlider = (CustomSlider)e.NewElement;

            customSlider.DragCompleted += CustomSlider_DragCompleted;

        }
    }


    private void CustomSlider_DragCompleted(object sender, EventArgs e)
    {
        Console.WriteLine("------CustomSlider_DragCompleted-------");
    }

}

Here , CustomSlider is my custom Slider in Xamarin.Forms . If later find the reason why TouchDragExit not woking will update here.

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • Thanks for the answer, the interesting thing is that the OnControlValueChanged is triggered but the Slider_TouchDragExit is never reached. I have no idea why. Do you have any idea where shall I start to debug? Thanks! – TheElderWisedom Oct 11 '19 at 11:39
  • @TheElderWisedom Sorry for not working about this method in Xamain.iOS after testing in local project. However there is a workaround to invoke Drag Complete method , I will update the answer . – Junior Jiang Oct 14 '19 at 06:28
  • @TheElderWisedom Hi ,I also have submitted it in GitHub . You also can follow it up [here](https://github.com/xamarin/Xamarin.Forms/issues/8017) . – Junior Jiang Oct 15 '19 at 08:15
  • Hello, yes I was able to solve it thanks to your post. You triggered an idea what I implemented and seems to work fine. I will update this thread with the solution I implemented and I will check the post you submitted to GitHub. – TheElderWisedom Oct 18 '19 at 12:45
  • @TheElderWisedom Glad found the solution . Later when sharing in answer, I will refer to it. – Junior Jiang Oct 21 '19 at 02:28