get { return false; }
}
- protected override void Build (StackLayout stackLayout)
+ protected override void Build(StackLayout stackLayout)
{
- base.Build (stackLayout);
+ base.Build(stackLayout);
- var isToggledContainer = new ValueViewContainer<Switch> (Test.Switch.IsToggled, new Switch (), "IsToggled", value => value.ToString ());
+ var isToggledContainer = new ValueViewContainer<Switch>(Test.Switch.IsToggled, new Switch(), "IsToggled", value => value.ToString());
var onColoredSwitch = new Switch() { OnColor = Color.HotPink };
onColorContainer.ContainerLayout.Children.Add(changeOnColorButton);
onColorContainer.ContainerLayout.Children.Add(clearOnColorButton);
+ var thumbColorSwitch = new Switch() { ThumbColor = Color.Yellow };
+ var thumbColorContainer = new ValueViewContainer<Switch>(Test.Switch.ThumbColor, thumbColorSwitch, nameof(Switch.ThumbColor), value => value.ToString());
+ var changeThumbColorButton = new Button { Text = "Change ThumbColor", Command = new Command(() => thumbColorSwitch.ThumbColor = Color.Azure) };
+ var clearThumbColorButton = new Button { Text = "Clear ThumbColor", Command = new Command(() => thumbColorSwitch.ThumbColor = Color.Default) };
+ thumbColorContainer.ContainerLayout.Children.Add(changeThumbColorButton);
+ thumbColorContainer.ContainerLayout.Children.Add(clearThumbColorButton);
+
Add(isToggledContainer);
Add(onColorContainer);
+ Add(thumbColorContainer);
}
}
}
\ No newline at end of file
[assembly: StyleProperty("-xf-orientation", typeof(StackLayout), nameof(StackLayout.OrientationProperty))]
[assembly: StyleProperty("-xf-visual", typeof(VisualElement), nameof(VisualElement.VisualProperty))]
[assembly: StyleProperty("-xf-vertical-text-alignment", typeof(Label), nameof(Label.VerticalTextAlignmentProperty))]
+[assembly: StyleProperty("-xf-thumb-color", typeof(Switch), nameof(Switch.ThumbColorProperty))]
//shell
[assembly: StyleProperty("-xf-flyout-background", typeof(Shell), nameof(Shell.FlyoutBackgroundColorProperty))]
public static readonly BindableProperty OnColorProperty = BindableProperty.Create(nameof(OnColor), typeof(Color), typeof(Switch), Color.Default);
+ public static readonly BindableProperty ThumbColorProperty = BindableProperty.Create(nameof(ThumbColor), typeof(Color), typeof(Switch), Color.Default);
+
public Color OnColor
{
get { return (Color)GetValue(OnColorProperty); }
set { SetValue(OnColorProperty, value); }
}
+ public Color ThumbColor
+ {
+ get { return (Color)GetValue(ThumbColorProperty); }
+ set { SetValue(ThumbColorProperty, value); }
+ }
readonly Lazy<PlatformConfigurationRegistry<Switch>> _platformConfigurationRegistry;
public enum Switch
{
IsToggled,
- OnColor
+ OnColor,
+ ThumbColor
}
public enum CheckBox
bool _disposed;
Drawable _defaultTrackDrawable;
string _defaultContentDescription;
+ ColorFilter _defaultThumbColorFilter;
public SwitchRenderer(Context context) : base(context)
{
aswitch.SetOnCheckedChangeListener(this);
SetNativeControl(aswitch);
_defaultTrackDrawable = aswitch.TrackDrawable;
+ _defaultThumbColorFilter = Control.ThumbDrawable.ColorFilter;
}
else
UpdateEnabled(); // Normally set by SetNativeControl, but not when the Control is reused.
e.NewElement.Toggled += HandleToggled;
Control.Checked = e.NewElement.IsToggled;
UpdateOnColor();
+ UpdateThumbColor();
}
}
if (e.PropertyName == Switch.OnColorProperty.PropertyName)
UpdateOnColor();
+ else if (e.PropertyName == Slider.ThumbColorProperty.PropertyName)
+ UpdateThumbColor();
}
void UpdateOnColor()
}
}
+ void UpdateThumbColor()
+ {
+ if (Element == null)
+ return;
+
+ Color thumbColor = Element.ThumbColor;
+ if (thumbColor.IsDefault)
+ Control.ThumbDrawable.SetColorFilter(_defaultThumbColorFilter);
+ else
+ Control.ThumbDrawable.SetColorFilter(thumbColor.ToAndroid(), PorterDuff.Mode.SrcIn);
+ }
+
void HandleToggled(object sender, EventArgs e)
{
Control.Checked = Element.IsToggled;
public class SwitchRenderer : ViewRenderer<Switch, ASwitch>, CompoundButton.IOnCheckedChangeListener
{
Drawable _defaultTrackDrawable;
+ ColorFilter _defaultThumbColorFilter;
public SwitchRenderer(Context context) : base(context)
{
aswitch.SetOnCheckedChangeListener(this);
SetNativeControl(aswitch);
_defaultTrackDrawable = Control.TrackDrawable;
+ _defaultThumbColorFilter = Control.ThumbDrawable.ColorFilter;
}
else
{
e.NewElement.Toggled += HandleToggled;
Control.Checked = e.NewElement.IsToggled;
UpdateOnColor();
+ UpdateThumbColor();
}
}
if (e.PropertyName == Switch.OnColorProperty.PropertyName)
UpdateOnColor();
+ else if (e.PropertyName == Slider.ThumbColorProperty.PropertyName)
+ UpdateThumbColor();
}
void UpdateOnColor()
}
}
+ void UpdateThumbColor()
+ {
+ if (Element == null)
+ return;
+
+ Color thumbColor = Element.ThumbColor;
+ if (thumbColor.IsDefault)
+ Control.ThumbDrawable.SetColorFilter(_defaultThumbColorFilter);
+ else
+ Control.ThumbDrawable.SetColorFilter(thumbColor.ToAndroid(), PorterDuff.Mode.SrcIn);
+ }
+
void HandleToggled(object sender, EventArgs e)
{
Control.Checked = Element.IsToggled;
{
Brush _originalOnHoverColor;
Brush _originalOnColorBrush;
+ Brush _originalThumbOnBrush;
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
{
}
else if (e.PropertyName == Switch.OnColorProperty.PropertyName)
UpdateOnColor();
+ else if (e.PropertyName == Switch.ThumbColorProperty.PropertyName)
+ UpdateThumbColor();
}
protected override bool PreventGestureBubbling { get; set; } = true;
void OnControlLoaded(object sender, RoutedEventArgs e)
{
UpdateOnColor();
+ UpdateThumbColor();
Control.Loaded -= OnControlLoaded;
}
else
rect.Fill = _originalOnColorBrush;
}
+
+ void UpdateThumbColor()
+ {
+ if (Control == null)
+ return;
+
+ var grid = Control.GetFirstDescendant<Windows.UI.Xaml.Controls.Grid>();
+ if (grid == null)
+ return;
+
+ ObjectKeyFrame frame = Windows.UI.Xaml.VisualStateManager.GetVisualStateGroups(grid)
+ .First(g => g.Name == "CommonStates")
+ .States.First(s => s.Name == "PointerOver")
+ .Storyboard.Children.OfType<ObjectAnimationUsingKeyFrames>().First(
+ t => Storyboard.GetTargetName(t) == "SwitchKnobOn" &&
+ Storyboard.GetTargetProperty(t) == "Fill")
+ .KeyFrames.First();
+
+ if (_originalThumbOnBrush == null)
+ _originalThumbOnBrush = (Brush)frame.Value;
+
+ if (!Element.ThumbColor.IsDefault)
+ frame.Value = new SolidColorBrush(Element.ThumbColor.ToWindowsColor())
+ {
+ Opacity = _originalThumbOnBrush.Opacity
+ };
+ else
+ frame.Value = _originalThumbOnBrush;
+
+ var thumb = (Ellipse)grid.FindName("SwitchKnobOn");
+ if (_originalThumbOnBrush == null)
+ _originalThumbOnBrush = thumb.Fill;
+
+ if (!Element.ThumbColor.IsDefault)
+ thumb.Fill = new SolidColorBrush(Element.ThumbColor.ToWindowsColor());
+ else
+ thumb.Fill = _originalThumbOnBrush;
+ }
}
}
public class SwitchRenderer : ViewRenderer<Switch, UISwitch>
{
UIColor _defaultOnColor;
+ UIColor _defaultThumbColor;
+
protected override void Dispose(bool disposing)
{
if (disposing)
}
_defaultOnColor = UISwitch.Appearance.OnTintColor;
+ _defaultThumbColor = UISwitch.Appearance.ThumbTintColor;
Control.On = Element.IsToggled;
e.NewElement.Toggled += OnElementToggled;
UpdateOnColor();
+ UpdateThumbColor();
}
base.OnElementChanged(e);
}
}
+ void UpdateThumbColor()
+ {
+ if (Element == null)
+ return;
+
+ Color thumbColor = Element.ThumbColor;
+ Control.ThumbTintColor = thumbColor.IsDefault ? _defaultThumbColor : thumbColor.ToUIColor();
+ }
+
void OnControlValueChanged(object sender, EventArgs e)
{
((IElementController)Element).SetValueFromRenderer(Switch.IsToggledProperty, Control.On);
if (e.PropertyName == Switch.OnColorProperty.PropertyName)
UpdateOnColor();
+ if (e.PropertyName == Switch.ThumbColorProperty.PropertyName)
+ UpdateThumbColor();
}
}
}
\ No newline at end of file