Implement more WinUI SwitchHandler properties (#1007)
* Implement more properties in WinUI SwitchHandler * Updated WinUI SwitchHandler * Updated WinUI SwitchHandler * Updated variables naming * Add some helpers to deal with Resources on WinUI * Switch to using resources for winui handler * Fix build error * Fixed issue setting the OnColor * Fix NRE * Don't forget to call SetupDefaults * Fix for OnColor the first time Co-authored-by: Jonathan Dick <jondick@gmail.com> Co-authored-by: Rui Marinho <me@ruimarinho.net>
This commit is contained in:
Родитель
4743decbc7
Коммит
c7b06faed7
|
@ -86,6 +86,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP
|
|||
Control.UpdateFlowDirection(Element);
|
||||
}
|
||||
|
||||
[PortHandler]
|
||||
void UpdateOnColor()
|
||||
{
|
||||
if (Control == null)
|
||||
|
@ -156,6 +157,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP
|
|||
}
|
||||
}
|
||||
|
||||
[PortHandler]
|
||||
void UpdateThumbColor()
|
||||
{
|
||||
if (Control == null)
|
||||
|
|
|
@ -16,6 +16,16 @@
|
|||
Style="{StaticResource Headline}"/>
|
||||
<Switch
|
||||
IsEnabled="False"/>
|
||||
<Label
|
||||
Text="OnColor"
|
||||
Style="{StaticResource Headline}"/>
|
||||
<Switch
|
||||
OnColor="Red"/>
|
||||
<Label
|
||||
Text="ThumbColor"
|
||||
Style="{StaticResource Headline}"/>
|
||||
<Switch
|
||||
ThumbColor="Orange"/>
|
||||
</VerticalStackLayout>
|
||||
</views:BasePage.Content>
|
||||
</views:BasePage>
|
|
@ -8,10 +8,14 @@ namespace Microsoft.Maui.Controls
|
|||
{
|
||||
get
|
||||
{
|
||||
#if WINDOWS
|
||||
return OnColor;
|
||||
#else
|
||||
if (IsToggled)
|
||||
return OnColor;
|
||||
|
||||
return null;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,34 @@
|
|||
#nullable enable
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using WResourceDictionary = Microsoft.UI.Xaml.ResourceDictionary;
|
||||
|
||||
namespace Microsoft.Maui.Handlers
|
||||
{
|
||||
public partial class SwitchHandler : ViewHandler<ISwitch, ToggleSwitch>
|
||||
{
|
||||
WResourceDictionary? _originalResources;
|
||||
|
||||
protected override ToggleSwitch CreateNativeView() => new ToggleSwitch();
|
||||
|
||||
void SetupDefaults(ToggleSwitch nativeView)
|
||||
{
|
||||
_originalResources = nativeView?.CloneResources();
|
||||
}
|
||||
|
||||
public static void MapIsOn(SwitchHandler handler, ISwitch view)
|
||||
{
|
||||
handler.NativeView?.UpdateIsToggled(view);
|
||||
}
|
||||
|
||||
[MissingMapper]
|
||||
public static void MapTrackColor(SwitchHandler handler, ISwitch view) { }
|
||||
public static void MapTrackColor(SwitchHandler handler, ISwitch view)
|
||||
{
|
||||
handler.NativeView?.UpdateTrackColor(view, handler._originalResources);
|
||||
}
|
||||
|
||||
[MissingMapper]
|
||||
public static void MapThumbColor(SwitchHandler handler, ISwitch view) { }
|
||||
public static void MapThumbColor(SwitchHandler handler, ISwitch view)
|
||||
{
|
||||
handler.NativeView?.UpdateThumbColor(view, handler._originalResources);
|
||||
}
|
||||
|
||||
protected override void DisconnectHandler(ToggleSwitch nativeView)
|
||||
{
|
||||
|
@ -26,6 +39,7 @@ namespace Microsoft.Maui.Handlers
|
|||
protected override void ConnectHandler(ToggleSwitch nativeView)
|
||||
{
|
||||
base.ConnectHandler(nativeView);
|
||||
SetupDefaults(nativeView);
|
||||
nativeView.Toggled += OnToggled;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ namespace Microsoft.Maui
|
|||
|
||||
public static Task<IImageSourceServiceResult<NativeImage>?> GetNativeImageAsync(this IImageSource imageSource, IMauiContext mauiContext)
|
||||
{
|
||||
if (imageSource == null)
|
||||
return new Task<IImageSourceServiceResult<NativeImage>?>(() => null);
|
||||
var services = mauiContext.Services;
|
||||
var provider = services.GetRequiredService<IImageSourceServiceProvider>();
|
||||
var imageSourceService = provider.GetRequiredImageSourceService(imageSource);
|
||||
|
|
|
@ -78,6 +78,7 @@ namespace Microsoft.Maui
|
|||
{
|
||||
var child = VisualTreeHelper.GetChild(parent, i);
|
||||
var controlName = child.GetValue(FrameworkElement.NameProperty) as string;
|
||||
|
||||
if (controlName == elementName && child is T t)
|
||||
yield return t;
|
||||
else
|
||||
|
@ -102,6 +103,30 @@ namespace Microsoft.Maui
|
|||
return null;
|
||||
}
|
||||
|
||||
internal static ResourceDictionary CloneResources(this FrameworkElement element)
|
||||
{
|
||||
var rd = new ResourceDictionary();
|
||||
|
||||
foreach (var r in element.Resources)
|
||||
rd.TryAdd(r.Key, r.Value);
|
||||
|
||||
return rd;
|
||||
}
|
||||
|
||||
internal static void TryUpdateResource(this FrameworkElement element, object newValue, params string[] keys)
|
||||
{
|
||||
var rd = element?.Resources;
|
||||
|
||||
if (rd == null)
|
||||
return;
|
||||
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (rd?.ContainsKey(key) ?? false)
|
||||
rd[key] = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
static DependencyProperty? GetForegroundProperty(FrameworkElement element)
|
||||
{
|
||||
if (element is Control)
|
||||
|
@ -131,6 +156,7 @@ namespace Microsoft.Maui
|
|||
for (int i = 0; i < myChildrenCount; i++)
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(parent, i);
|
||||
|
||||
if (child is T t)
|
||||
yield return t;
|
||||
else
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
using Microsoft.UI.Xaml.Controls;
|
||||
using WSolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;
|
||||
using WResourceDictionary = Microsoft.UI.Xaml.ResourceDictionary;
|
||||
|
||||
namespace Microsoft.Maui
|
||||
{
|
||||
|
@ -6,7 +8,60 @@ namespace Microsoft.Maui
|
|||
{
|
||||
public static void UpdateIsToggled(this ToggleSwitch toggleSwitch, ISwitch view)
|
||||
{
|
||||
toggleSwitch.IsOn = view.IsOn;
|
||||
if (toggleSwitch != null)
|
||||
{
|
||||
toggleSwitch.IsOn = view?.IsOn ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateTrackColor(this ToggleSwitch toggleSwitch, ISwitch view, WResourceDictionary? originalResources = null)
|
||||
{
|
||||
if (toggleSwitch == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (view.TrackColor == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (view.TrackColor != null)
|
||||
{
|
||||
toggleSwitch.TryUpdateResource(
|
||||
view.TrackColor.ToNative() ?? originalResources?["ToggleSwitchFillOff"] as WSolidColorBrush ?? new WSolidColorBrush(),
|
||||
"ToggleSwitchFillOn",
|
||||
"ToggleSwitchFillOnPointerOver",
|
||||
"ToggleSwitchFillOnPressed",
|
||||
"ToggleSwitchFillOnDisabled");
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateThumbColor(this ToggleSwitch toggleSwitch, ISwitch view, WResourceDictionary? originalResources = null)
|
||||
{
|
||||
if (toggleSwitch == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (view.ThumbColor == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (view.ThumbColor != null)
|
||||
{
|
||||
toggleSwitch.TryUpdateResource(
|
||||
view.ThumbColor.ToNative() ?? originalResources?["ToggleSwitchKnobFillOff"] as WSolidColorBrush ?? new WSolidColorBrush(),
|
||||
"ToggleSwitchKnobFillOnPointerOver",
|
||||
"ToggleSwitchKnobFillOn",
|
||||
"ToggleSwitchKnobFillOnPressed",
|
||||
"ToggleSwitchKnobFillOnDisabled",
|
||||
"ToggleSwitchKnobFillOffPointerOver",
|
||||
"ToggleSwitchKnobFillOff",
|
||||
"ToggleSwitchKnobFillOffPressed",
|
||||
"ToggleSwitchKnobFillOffDisabled");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче