Add support for changing the true color for a toggle switch

This commit is contained in:
Shawn Kendrot 2016-12-13 17:28:58 -07:00
Родитель dead93ad2b
Коммит 70f497c240
3 изменённых файлов: 131 добавлений и 0 удалений

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

@ -0,0 +1,88 @@
using Windows.UI.Xaml.Controls;
using FormsCommunityToolkit.Effects.UWP.Effects;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.UWP;
using Windows.UI.Xaml.Media;
using System;
using Windows.UI.Xaml;
using System.Linq;
using Windows.UI.Xaml.Media.Animation;
[assembly: ExportEffect(typeof(ChangeColorSwitchEffect), nameof(ChangeColorSwitchEffect))]
namespace FormsCommunityToolkit.Effects.UWP.Effects
{
[Preserve]
public class ChangeColorSwitchEffect : PlatformEffect
{
private Windows.UI.Color _trueColor;
private Windows.UI.Color _falseColor;
protected override void OnAttached()
{
var color = (Color)Element.GetValue(ChangeColorEffect.TrueColorProperty);
_trueColor = ConvertColor(color);
// currently not supported
color = (Color)Element.GetValue(ChangeColorEffect.FalseColorProperty);
_falseColor = ConvertColor(color);
var toggleSwitch = Control as ToggleSwitch;
if (toggleSwitch != null)
{
toggleSwitch.Loaded -= OnSwitchLoaded;
toggleSwitch.Loaded += OnSwitchLoaded;
}
}
protected override void OnDetached()
{
var toggleSwitch = Control as ToggleSwitch;
if (toggleSwitch != null)
{
toggleSwitch.Loaded -= OnSwitchLoaded;
}
}
private void OnSwitchLoaded(object sender, RoutedEventArgs e)
{
var toggleSwitch = Control as ToggleSwitch;
var grid = toggleSwitch.GetChildOfType<Windows.UI.Xaml.Controls.Grid>();
var groups = VisualStateManager.GetVisualStateGroups(grid);
foreach (var group in groups)
{
if (group.Name != "CommonStates") continue;
foreach (var state in group.States)
{
if (state.Name != "PointerOver") continue;
foreach (var timeline in state.Storyboard.Children.OfType<ObjectAnimationUsingKeyFrames>())
{
var property = Storyboard.GetTargetProperty(timeline);
var target = Storyboard.GetTargetName(timeline);
if ((target == "SwitchKnobBounds") && (property == "Fill"))
{
var frame = timeline.KeyFrames.First();
frame.Value = new SolidColorBrush(_trueColor) { Opacity = .7 };
break;
}
}
}
}
var rect = toggleSwitch.GetChildByName("SwitchKnobBounds") as Windows.UI.Xaml.Shapes.Rectangle;
if (rect != null)
{
rect.Fill = new SolidColorBrush(_trueColor);
}
toggleSwitch.Loaded -= OnSwitchLoaded;
}
private Windows.UI.Color ConvertColor(Xamarin.Forms.Color color)
{
return Windows.UI.Color.FromArgb((byte)(color.A * 255), (byte)(color.R * 255), (byte)(color.G * 255), (byte)(color.B * 255));
}
}
}

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

@ -107,11 +107,13 @@
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="Effects\ChangeColorSwitchEffect.cs" />
<Compile Include="Effects\CapitalizeKeyboardEffect.cs" />
<Compile Include="Effects\MultiLineLabelEffect.cs" />
<Compile Include="Effects\RemoveBorderEffect.cs" />
<Compile Include="Effects\ViewBlurEffect.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VisualTreeExtensions.cs" />
<EmbeddedResource Include="Properties\Organon.XForms.Effects.UWP.rd.xml" />
</ItemGroup>
<ItemGroup>

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

@ -0,0 +1,41 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace FormsCommunityToolkit.Effects.UWP
{
static class VisualTreeExtensions
{
public static T GetChildOfType<T>(this DependencyObject obj) where T: DependencyObject
{
if (obj == null) return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
public static FrameworkElement GetChildByName(this FrameworkElement obj, string name)
{
if (obj == null) return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i) as FrameworkElement;
if (child == null) continue;
if(child.Name == name)
{
return child;
}
var result = GetChildByName(child, name);
if (result != null) return result;
}
return null;
}
}
}