Hello, my Xamarin friends!

Today, I am here with another new post in which I am going to tell you how to customize the Slider Lock. So, let's start.

So, we will first create a class whose name "SlideToActView" and then we will write a code in "C #".

  1. namespace XamarinControlsApp.SliderLock
  2. {
  3. public class SlideToActView : AbsoluteLayout
  4. {
  5. public static readonly BindableProperty ThumbProperty =
  6. BindableProperty.Create(
  7. nameof(Thumb), typeof(View), typeof(SlideToActView),
  8. defaultValue: default(View));
  9. public View Thumb
  10. {
  11. get { return (View)GetValue(ThumbProperty); }
  12. set { SetValue(ThumbProperty, value); }
  13. }
  14. public static readonly BindableProperty TrackBarProperty =
  15. BindableProperty.Create(
  16. nameof(TrackBar),
  17. typeof(View),
  18. typeof(SlideToActView),
  19. defaultValue: default(View));
  20. public View TrackBar
  21. {
  22. get { return (View)GetValue(TrackBarProperty); }
  23. set { SetValue(TrackBarProperty, value); }
  24. }
  25. public static readonly BindableProperty FillBarProperty =
  26. BindableProperty.Create(
  27. nameof(FillBar), typeof(View), typeof(SlideToActView),
  28. defaultValue: default(View));
  29. public View FillBar
  30. {
  31. get { return (View)GetValue(FillBarProperty); }
  32. set { SetValue(FillBarProperty, value); }
  33. }
  34. private PanGestureRecognizer _panGesture = new PanGestureRecognizer();
  35. private View _gestureListener;
  36. public SlideToActView()
  37. {
  38. _panGesture.PanUpdated += OnPanGestureUpdated;
  39. SizeChanged += OnSizeChanged;
  40. _gestureListener = new ContentView { BackgroundColor = Color.White, Opacity = 0.05 };
  41. _gestureListener.GestureRecognizers.Add(_panGesture);
  42. }
  43. public event EventHandler SlideCompleted;
  44. private const double _fadeEffect = 0.5;
  45. private const uint _animLength = 50;
  46. async void OnPanGestureUpdated(object sender, PanUpdatedEventArgs e)
  47. {
  48. if (Thumb == null || TrackBar == null || FillBar == null)
  49. return;
  50. switch (e.StatusType)
  51. {
  52. case GestureStatus.Started:
  53. await TrackBar.FadeTo(_fadeEffect, _animLength);
  54. break;
  55. case GestureStatus.Running:
  56. // Translate and ensure we don't pan beyond the wrapped user interface element bounds.
  57. var x = Math.Max(0, e.TotalX);
  58. if (x > (Width - Thumb.Width))
  59. x = (Width - Thumb.Width);
  60. //Uncomment this if you want only forward dragging.
  61. //if (e.TotalX < Thumb.TranslationX)
  62. // return;
  63. Thumb.TranslationX = x;
  64. SetLayoutBounds(FillBar, new Rectangle(0, 0, x + Thumb.Width / 2, this.Height));
  65. break;
  66. case GestureStatus.Completed:
  67. var posX = Thumb.TranslationX;
  68. SetLayoutBounds(FillBar, new Rectangle(0, 0, 0, this.Height));
  69. // Reset translation applied during the pan
  70. await Task.WhenAll(new Task[]{
  71. TrackBar.FadeTo(1, _animLength),
  72. Thumb.TranslateTo(0, 0, _animLength * 2, Easing.CubicIn),
  73. });
  74. if (posX >= (Width - Thumb.Width - 10/* keep some margin for error*/))
  75. SlideCompleted?.Invoke(this, EventArgs.Empty);
  76. break;
  77. }
  78. }
  79. void OnSizeChanged(object sender, EventArgs e)
  80. {
  81. if (Width == 0 || Height == 0)
  82. return;
  83. if (Thumb == null || TrackBar == null || FillBar == null)
  84. return;
  85. Children.Clear();
  86. SetLayoutFlags(TrackBar, AbsoluteLayoutFlags.SizeProportional);
  87. SetLayoutBounds(TrackBar, new Rectangle(0, 0, 1, 1));
  88. Children.Add(TrackBar);
  89. SetLayoutFlags(FillBar, AbsoluteLayoutFlags.None);
  90. SetLayoutBounds(FillBar, new Rectangle(0, 0, 0, this.Height));
  91. Children.Add(FillBar);
  92. SetLayoutFlags(Thumb, AbsoluteLayoutFlags.None);
  93. SetLayoutBounds(Thumb, new Rectangle(0, 0, this.Width / 5, this.Height));
  94. Children.Add(Thumb);
  95. SetLayoutFlags(_gestureListener, AbsoluteLayoutFlags.SizeProportional);
  96. SetLayoutBounds(_gestureListener, new Rectangle(0, 0, 1, 1));
  97. Children.Add(_gestureListener);
  98. }
  99. }
  100. }

Please make sure to add view reference....

xmlns:local="clr-namespace:XamarinControlsApp.SliderLock"

Then, write the XAML code in main page.

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local1="clr-namespace:XamarinControlsApp"
  5. x:Class="XamarinControlsApp.MainPage"
  6. xmlns:local="clr-namespace:XamarinControlsApp.SliderLock"
  7. Title="Slider Lock">
  8. <StackLayout Margin="35">
  9. <local:SlideToActView HeightRequest="50"
  10. SlideCompleted="Handle_SlideCompleted">
  11. <local:SlideToActView.Thumb>
  12. <Frame CornerRadius="15"
  13. HasShadow="false"
  14. BackgroundColor="Silver"
  15. Padding="0">
  16. <Image Source="icon.png"
  17. HorizontalOptions="Center"
  18. VerticalOptions="Center"
  19. HeightRequest="35"
  20. WidthRequest="35" />
  21. </Frame>
  22. </local:SlideToActView.Thumb>
  23. <local:SlideToActView.TrackBar>
  24. <Frame CornerRadius="15"
  25. HasShadow="false"
  26. BackgroundColor="Gray"
  27. Padding="0">
  28. <Label Text="Sliding to Unlock"
  29. HorizontalOptions="CenterAndExpand"
  30. VerticalOptions="CenterAndExpand" />
  31. </Frame>
  32. </local:SlideToActView.TrackBar>
  33. <local:SlideToActView.FillBar>
  34. <Frame CornerRadius="15"
  35. HasShadow="false"
  36. BackgroundColor="Green"
  37. Padding="0" />
  38. </local:SlideToActView.FillBar>
  39. </local:SlideToActView>
  40. <Label x:Name="MessageLbl"
  41. FontAttributes="Bold"
  42. TextColor="Green" />
  43. </StackLayout>
  44. </ContentPage>
And then, write the following code for sliding event.
  1. public async void Handle_SlideCompleted(object sender, System.EventArgs e)
  2. {
  3. MessageLbl.Text = "Unlock Successfully!!";
  4. await Navigation.PushAsync(new NextPage());
  5. }

You should have your Slider Lock working!! If you want to watch a related video, click here Custom Slider Lock.