Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Compatibility/Core/src/Windows/ActivityIndicatorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Xaml;
using WBinding = Microsoft.UI.Xaml.Data.Binding;
using WBrush = Microsoft.UI.Xaml.Media.Brush;

namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP
{
Expand Down Expand Up @@ -45,7 +47,7 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE

void OnControlLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_foregroundDefault = Control.GetForegroundCache();
_foregroundDefault = GetForegroundCache();
UpdateColor();
}

Expand All @@ -56,7 +58,7 @@ void UpdateColor()

if (color.IsDefault())
{
Control.RestoreForegroundCache(_foregroundDefault);
RestoreForegroundCache();
}
else
{
Expand All @@ -69,5 +71,23 @@ void UpdateIsRunning()
{
Control.ElementOpacity = Element.IsRunning ? Element.Opacity : 0;
}

private object GetForegroundCache()
{
return Control.GetBindingExpression(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty)?.ParentBinding
?? Control.GetValue(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty);
}

private void RestoreForegroundCache()
{
if (_foregroundDefault is WBinding binding)
{
Control.SetBinding(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty, binding);
}
else
{
Control.SetValue(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty, (WBrush)_foregroundDefault);
}
}
}
}
82 changes: 0 additions & 82 deletions src/Compatibility/Core/src/Windows/FrameworkElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,94 +7,12 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using WBinding = Microsoft.UI.Xaml.Data.Binding;
using WBindingExpression = Microsoft.UI.Xaml.Data.BindingExpression;
using WBrush = Microsoft.UI.Xaml.Media.Brush;

namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP
{
[PortHandler]
internal static class FrameworkElementExtensions
{
static readonly Lazy<ConcurrentDictionary<Type, DependencyProperty>> ForegroundProperties =
new Lazy<ConcurrentDictionary<Type, DependencyProperty>>(() => new ConcurrentDictionary<Type, DependencyProperty>());

public static WBrush GetForeground(this FrameworkElement element)
{
if (element == null)
throw new ArgumentNullException("element");

return (WBrush)element.GetValue(GetForegroundProperty(element));
}

public static WBinding GetForegroundBinding(this FrameworkElement element)
{
WBindingExpression expr = element.GetBindingExpression(GetForegroundProperty(element));
if (expr == null)
return null;

return expr.ParentBinding;
}

public static object GetForegroundCache(this FrameworkElement element)
{
WBinding binding = GetForegroundBinding(element);
if (binding != null)
return binding;

return GetForeground(element);
}

public static void RestoreForegroundCache(this FrameworkElement element, object cache)
{
var binding = cache as WBinding;
if (binding != null)
SetForeground(element, binding);
else
SetForeground(element, (WBrush)cache);
}

public static void SetForeground(this FrameworkElement element, WBrush foregroundBrush)
{
if (element == null)
throw new ArgumentNullException("element");

element.SetValue(GetForegroundProperty(element), foregroundBrush);
}

public static void SetForeground(this FrameworkElement element, WBinding binding)
{
if (element == null)
throw new ArgumentNullException("element");

element.SetBinding(GetForegroundProperty(element), binding);
}

static DependencyProperty GetForegroundProperty(FrameworkElement element)
{
if (element is Control)
return Control.ForegroundProperty;
if (element is TextBlock)
return TextBlock.ForegroundProperty;

Type type = element.GetType();

DependencyProperty foregroundProperty;
if (!ForegroundProperties.Value.TryGetValue(type, out foregroundProperty))
{
FieldInfo field = ReflectionExtensions.GetFields(type).FirstOrDefault(f => f.Name == "ForegroundProperty");
if (field == null)
throw new ArgumentException("type is not a Foregroundable type");

var property = (DependencyProperty)field.GetValue(null);
ForegroundProperties.Value.TryAdd(type, property);

return property;
}

return foregroundProperty;
}

internal static IEnumerable<T> GetChildren<T>(this DependencyObject parent) where T : DependencyObject
{
int myChildrenCount = VisualTreeHelper.GetChildrenCount(parent);
Expand Down
24 changes: 22 additions & 2 deletions src/Compatibility/Core/src/Windows/ProgressBarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.Maui.Graphics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls.Primitives;
using WBinding = Microsoft.UI.Xaml.Data.Binding;
using WBrush = Microsoft.UI.Xaml.Media.Brush;

namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP
{
Expand Down Expand Up @@ -70,7 +72,7 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE

void OnControlLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_foregroundDefault = Control.GetForegroundCache();
_foregroundDefault = GetForegroundCache();
UpdateProgressColor();
}

Expand All @@ -81,7 +83,7 @@ void UpdateProgressColor()

if (color.IsDefault())
{
Control.RestoreForegroundCache(_foregroundDefault);
RestoreForegroundCache();
}
else
{
Expand All @@ -99,5 +101,23 @@ void UpdateFlowDirection()
{
Control.UpdateFlowDirection(Element);
}

private object GetForegroundCache()
{
return Control.GetBindingExpression(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty)?.ParentBinding
?? Control.GetValue(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty);
}

private void RestoreForegroundCache()
{
if (_foregroundDefault is WBinding binding)
{
Control.SetBinding(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty, binding);
}
else
{
Control.SetValue(Microsoft.UI.Xaml.Controls.Control.ForegroundProperty, (WBrush)_foregroundDefault);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Controls/src/Core/BindablePropertyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ static Type GetControlType(string typeName)
BindableProperty ConvertFrom(Type type, string propertyName, IXmlLineInfo lineinfo)
{
string name = propertyName + "Property";
FieldInfo bpinfo = type.GetField(fi => fi.Name == name && fi.IsStatic && fi.IsPublic && fi.FieldType == typeof(BindableProperty));
if (bpinfo == null)
FieldInfo bpinfo = type.GetField(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (bpinfo == null || bpinfo.FieldType != typeof(BindableProperty))
throw new XamlParseException($"Can't resolve {name} on {type.Name}", lineinfo);
var bp = bpinfo.GetValue(null) as BindableProperty;
var isObsolete = bpinfo.GetCustomAttribute<ObsoleteAttribute>() != null;
Expand Down

This file was deleted.

24 changes: 0 additions & 24 deletions src/Core/src/Platform/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Microsoft.Maui.Platform
{
internal static class ReflectionExtensions
{
public static FieldInfo? GetField(this Type type, Func<FieldInfo, bool> predicate)
{
return GetFields(type).FirstOrDefault(predicate);
}

public static IEnumerable<FieldInfo> GetFields(this Type type)
{
return GetParts(type, i => i.DeclaredFields);
}

internal static object[]? GetCustomAttributesSafe(this Assembly assembly, Type attrType)
{
try
Expand All @@ -38,17 +26,5 @@ public static bool IsInstanceOfType(this Type self, object o)
{
return self.IsAssignableFrom(o.GetType());
}

static IEnumerable<T> GetParts<T>(Type type, Func<TypeInfo, IEnumerable<T>> selector)
{
Type? t = type;
while (t != null)
{
TypeInfo ti = t.GetTypeInfo();
foreach (T f in selector(ti))
yield return f;
t = ti.BaseType;
}
}
}
}
18 changes: 18 additions & 0 deletions src/Core/src/Platform/Windows/ActivityIndicatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#nullable enable
using System;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Primitives;
using Microsoft.UI.Xaml.Controls;
using WBinding = Microsoft.UI.Xaml.Data.Binding;
using WBrush = Microsoft.UI.Xaml.Media.Brush;

namespace Microsoft.Maui.Platform
{
Expand Down Expand Up @@ -50,5 +53,20 @@ public static void UpdateHeight(this ProgressRing platformActivityIndicator, IAc
}
// Otherwise, don't set it to anything (even NaN) because it will try to fill all the space you give it
}

private static void RestoreForegroundCache(this ProgressRing progressRing, object cache)
{
if (progressRing == null)
throw new ArgumentNullException(nameof(progressRing));

if (cache is WBinding binding)
{
progressRing.SetBinding(Control.ForegroundProperty, binding);
}
else
{
progressRing.SetValue(Control.ForegroundProperty, (WBrush)cache);
}
}
}
}
Loading