* Added OnColor property for changing switchcell color

* Added logic to apply color to Switch

* Used nameof on the OnColor property

* [UWP] support for UWP
This commit is contained in:
Nathaniel Nunes 2018-11-27 18:02:07 +05:30 коммит произвёл Rui Marinho
Родитель 6f76390a19
Коммит eee78fe2b7
5 изменённых файлов: 147 добавлений и 0 удалений

Просмотреть файл

@ -8,6 +8,7 @@ namespace Xamarin.Forms.Controls
{
public string Label { get; set; }
public bool SwitchOn { get; set; }
public Color OnColor { get; set; }
}
public class SwitchCellListPage : ContentPage
@ -23,6 +24,7 @@ namespace Xamarin.Forms.Controls
Bindings = {
{SwitchCell.TextProperty, new Binding ("Label")},
{SwitchCell.OnProperty, new Binding ("SwitchOn")},
{SwitchCell.OnColorProperty, new Binding ("OnColor")},
}
};
@ -33,6 +35,7 @@ namespace Xamarin.Forms.Controls
ItemsSource = Enumerable.Range (0, 100).Select (i => new SwitchCellItem {
Label = "Label " + i,
SwitchOn = i % 2 == 0 ? false : true,
OnColor = i % 2 == 0 ? Color.Firebrick : Color.GreenYellow
}),
ItemTemplate = dataTemplate
};

Просмотреть файл

@ -12,6 +12,14 @@ namespace Xamarin.Forms
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(SwitchCell), default(string));
public static readonly BindableProperty OnColorProperty = BindableProperty.Create(nameof(OnColor), typeof(Color), typeof(SwitchCell), Color.Default);
public Color OnColor
{
get { return (Color)GetValue(OnColorProperty); }
set { SetValue(OnColorProperty, value); }
}
public bool On
{
get { return (bool)GetValue(OnProperty); }

Просмотреть файл

@ -3,12 +3,16 @@ using Android.Content;
using Android.Views;
using AView = Android.Views.View;
using ASwitch = Android.Widget.Switch;
using Android.OS;
using Android.Graphics;
using Android.Graphics.Drawables;
namespace Xamarin.Forms.Platform.Android
{
public class SwitchCellRenderer : CellRenderer
{
SwitchCellView _view;
Drawable _defaultTrackDrawable;
protected override AView GetCellCore(Cell item, AView convertView, ViewGroup parent, Context context)
{
@ -19,11 +23,16 @@ namespace Xamarin.Forms.Platform.Android
_view.Cell = cell;
var aSwitch = _view.AccessoryView as ASwitch;
if (aSwitch != null)
_defaultTrackDrawable = aSwitch.TrackDrawable;
UpdateText();
UpdateChecked();
UpdateHeight();
UpdateIsEnabled(_view, cell);
UpdateFlowDirection();
UpdateOnColor(_view, cell);
return _view;
}
@ -33,13 +42,18 @@ namespace Xamarin.Forms.Platform.Android
if (args.PropertyName == SwitchCell.TextProperty.PropertyName)
UpdateText();
else if (args.PropertyName == SwitchCell.OnProperty.PropertyName)
{
UpdateChecked();
UpdateOnColor(_view, (SwitchCell)sender);
}
else if (args.PropertyName == "RenderHeight")
UpdateHeight();
else if (args.PropertyName == Cell.IsEnabledProperty.PropertyName)
UpdateIsEnabled(_view, (SwitchCell)sender);
else if (args.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection();
else if (args.PropertyName == SwitchCell.OnColorProperty.PropertyName)
UpdateOnColor(_view, (SwitchCell)sender);
}
void UpdateChecked()
@ -69,5 +83,31 @@ namespace Xamarin.Forms.Platform.Android
{
_view.MainText = ((SwitchCell)Cell).Text;
}
void UpdateOnColor(SwitchCellView cell, SwitchCell switchCell)
{
var aSwitch = cell.AccessoryView as ASwitch;
if (aSwitch != null)
{
if (switchCell.On)
{
if (switchCell.OnColor == Color.Default)
{
aSwitch.TrackDrawable = _defaultTrackDrawable;
}
else
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBean)
{
aSwitch.TrackDrawable.SetColorFilter(switchCell.OnColor.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
}
else
{
aSwitch.TrackDrawable.ClearColorFilter();
}
}
}
}
}

Просмотреть файл

@ -9,6 +9,7 @@ using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.UWP
@ -23,6 +24,7 @@ namespace Xamarin.Forms.Platform.UWP
internal static readonly BindableProperty MeasuredEstimateProperty = BindableProperty.Create("MeasuredEstimate", typeof(double), typeof(ListView), -1d);
readonly Lazy<ListView> _listView;
readonly PropertyChangedEventHandler _propertyChangedHandler;
Brush _defaultOnColor;
IList<MenuItem> _contextActions;
Windows.UI.Xaml.DataTemplate _currentTemplate;
@ -101,6 +103,7 @@ namespace Xamarin.Forms.Platform.UWP
if (lv != null)
{
lv.SetValue(MeasuredEstimateProperty, result.Height);
SetDafaultColor();
}
return result;
@ -138,6 +141,77 @@ namespace Xamarin.Forms.Platform.UWP
}
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection(Cell);
else if (e.PropertyName == SwitchCell.OnProperty.PropertyName ||
e.PropertyName == SwitchCell.OnColorProperty.PropertyName)
{
UpdateOnColor();
}
}
void UpdateOnColor()
{
if (!(Cell is SwitchCell switchCell))
return;
var color = switchCell.OnColor == Color.Default
? _defaultOnColor
: new SolidColorBrush(switchCell.OnColor.ToWindowsColor());
var nativeSwitch = FrameworkElementExtensions.GetFirstDescendant<ToggleSwitch>(this);
// change fill color in switch rectangle
var rects = nativeSwitch.GetDescendantsByName<Windows.UI.Xaml.Shapes.Rectangle>("SwitchKnobBounds");
foreach (var rect in rects)
rect.Fill = color;
// change color in animation on PointerOver
var grid = nativeSwitch.GetFirstDescendant<Windows.UI.Xaml.Controls.Grid>();
var gridVisualStateGroups = Windows.UI.Xaml.VisualStateManager.GetVisualStateGroups(grid);
Windows.UI.Xaml.VisualStateGroup vsGroup = null;
foreach (var visualGroup in gridVisualStateGroups)
{
if (visualGroup.Name == "CommonStates")
{
vsGroup = visualGroup;
break;
}
}
if (vsGroup == null)
return;
Windows.UI.Xaml.VisualState vState = null;
foreach (var visualState in vsGroup.States)
{
if (visualState.Name == "PointerOver")
{
vState = visualState;
break;
}
}
if (vState == null)
return;
var visualStates = vState.Storyboard.Children;
foreach (ObjectAnimationUsingKeyFrames item in visualStates)
{
if ((string)item.GetValue(Storyboard.TargetNameProperty) == "SwitchKnobBounds")
{
item.KeyFrames[0].Value = color;
break;
}
}
}
void SetDafaultColor()
{
if (_defaultOnColor == null && Cell is SwitchCell)
{
var nativeSwitch = FrameworkElementExtensions.GetFirstDescendant<ToggleSwitch>(this);
var rects = nativeSwitch.GetDescendantsByName<Windows.UI.Xaml.Shapes.Rectangle>("SwitchKnobBounds");
foreach (var rect in rects)
_defaultOnColor = rect.Fill;
UpdateOnColor();
}
}
void OnClick(object sender, PointerRoutedEventArgs e)

Просмотреть файл

@ -9,6 +9,8 @@ namespace Xamarin.Forms.Platform.iOS
{
const string CellName = "Xamarin.SwitchCell";
UIColor _defaultOnColor;
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var tvc = reusableCell as CellTableViewCell;
@ -32,6 +34,8 @@ namespace Xamarin.Forms.Platform.iOS
var boolCell = (SwitchCell)item;
_defaultOnColor = UISwitch.Appearance.OnTintColor;
tvc.Cell = item;
tvc.PropertyChanged += HandlePropertyChanged;
tvc.AccessoryView = uiSwitch;
@ -44,6 +48,7 @@ namespace Xamarin.Forms.Platform.iOS
UpdateBackground(tvc, item);
UpdateIsEnabled(tvc, boolCell);
UpdateFlowDirection(tvc, boolCell);
UpdateOnColor(tvc, boolCell);
return tvc;
}
@ -54,13 +59,18 @@ namespace Xamarin.Forms.Platform.iOS
var realCell = (CellTableViewCell)GetRealCell(boolCell);
if (e.PropertyName == SwitchCell.OnProperty.PropertyName)
{
((UISwitch)realCell.AccessoryView).SetState(boolCell.On, true);
UpdateOnColor(realCell, boolCell);
}
else if (e.PropertyName == SwitchCell.TextProperty.PropertyName)
realCell.TextLabel.Text = boolCell.Text;
else if (e.PropertyName == Cell.IsEnabledProperty.PropertyName)
UpdateIsEnabled(realCell, boolCell);
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection(realCell, boolCell);
else if (e.PropertyName == SwitchCell.OnColorProperty.PropertyName)
UpdateOnColor(realCell, boolCell);
}
void OnSwitchValueChanged(object sender, EventArgs eventArgs)
@ -97,5 +107,17 @@ namespace Xamarin.Forms.Platform.iOS
if (uiSwitch != null)
uiSwitch.Enabled = switchCell.IsEnabled;
}
void UpdateOnColor(CellTableViewCell cell, SwitchCell switchCell)
{
var uiSwitch = cell.AccessoryView as UISwitch;
if (uiSwitch != null)
{
if (switchCell.OnColor == Color.Default)
uiSwitch.OnTintColor = _defaultOnColor;
else
uiSwitch.OnTintColor = switchCell.OnColor.ToUIColor();
}
}
}
}