Skip to content

Instantly share code, notes, and snippets.

@jesulink2514
Created November 18, 2020 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesulink2514/0688771b995e91ec28cd2d16db8dccd6 to your computer and use it in GitHub Desktop.
Save jesulink2514/0688771b995e91ec28cd2d16db8dccd6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace FormsIcons.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TechieSwitch : ITechiesSwitchController
{
public static BindableProperty IsOnProperty = BindableProperty.Create(nameof(IsOn), typeof(bool),
typeof(TechieSwitch), false, BindingMode.TwoWay, propertyChanged: OnPropertyChanged);
public static BindableProperty ThumbBrushProperty = BindableProperty.Create(nameof(ThumbBrush), typeof(Brush),
typeof(TechieSwitch), SolidColorBrush.Blue, BindingMode.TwoWay, propertyChanged: OnBrushPropertyChanged);
public static BindableProperty BorderBrushProperty = BindableProperty.Create(nameof(BorderBrush), typeof(Brush),
typeof(TechieSwitch), SolidColorBrush.Blue, BindingMode.TwoWay, propertyChanged: OnBrushPropertyChanged);
public TechieSwitch()
{
InitializeComponent();
}
public bool IsOn
{
get => (bool)GetValue(IsOnProperty);
set => SetValue(IsOnProperty, value);
}
public Brush ThumbBrush
{
get => GetValue(ThumbBrushProperty) as Brush;
set => SetValue(ThumbBrushProperty, value);
}
public Brush BorderBrush
{
get => GetValue(BorderBrushProperty) as Brush;
set => SetValue(BorderBrushProperty,value);
}
private void ClickGestureRecognizer_OnClicked(object sender, EventArgs e)
{
IsOn = !IsOn;
}
private static void OnPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
{
(bindable as ITechiesSwitchController).SetUITo((bool)newvalue);
}
private static void OnBrushPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
{
var switchControl = bindable as TechieSwitch;
(bindable as ITechiesSwitchController).SetUITo(switchControl.IsOn);
}
async void ITechiesSwitchController.SetUITo(bool valueToShow)
{
var position = new Rectangle
{
Left = 20,
Top = 2,
Width = 16,
Height = 16
};
var uncheckedPosition = new Rectangle
{
Left = 2,
Top = 2,
Width = 16,
Height = 16
};
await Thumb.LayoutTo(valueToShow ? position : uncheckedPosition, 300, Easing.SinIn);
}
}
internal interface ITechiesSwitchController
{
void SetUITo(bool valueToShow);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment