Skip to content

Commit ac3986a

Browse files
committed
Added FrameworkElement.TryFindResource overload
1 parent f13d98d commit ac3986a

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/TokenizingTextBox/TokenizingTextBoxItem.AutoSuggestBox.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,7 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
105105
{
106106
// This can be expensive, could we optimize?
107107
// Also, this is changing the FontSize on the IconSource (which could be shared?)
108-
if (Owner.TryFindResource("TokenizingTextBoxIconFontSize", out object resource) &&
109-
resource is double fontSize)
110-
{
111-
fis.FontSize = fontSize;
112-
}
113-
else
114-
{
115-
fis.FontSize = 16;
116-
}
108+
fis.FontSize = Owner.TryFindResource("TokenizingTextBoxIconFontSize") as double? ?? 16;
117109
}
118110

119111
var iconBinding = new Binding()

Microsoft.Toolkit.Uwp.UI/Extensions/Tree/LogicalTree.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,10 @@ public static object FindResource(this FrameworkElement element, object resource
672672
/// </summary>
673673
/// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
674674
/// <param name="resourceKey">The resource key to search for.</param>
675-
/// <param name="value">The resulting value, if present.</param>
676-
/// <returns>Whether or not a value with the specified key has been found.</returns>
677-
public static bool TryFindResource(this FrameworkElement element, object resourceKey, out object? value)
675+
/// <returns>The requested resource, or <see langword="null"/> if it wasn't found.</returns>
676+
public static object? TryFindResource(this FrameworkElement element, object resourceKey)
678677
{
679-
value = null;
678+
object? value = null;
680679

681680
FrameworkElement? current = element;
682681

@@ -687,15 +686,34 @@ public static bool TryFindResource(this FrameworkElement element, object resourc
687686
{
688687
if (current.Resources?.TryGetValue(resourceKey, out value) == true)
689688
{
690-
return true;
689+
return value;
691690
}
692691

693692
current = current.Parent as FrameworkElement;
694693
}
695694
while (current is not null);
696695

697696
// Finally try application resources
698-
return Application.Current?.Resources?.TryGetValue(resourceKey, out value) == true;
697+
_ = Application.Current?.Resources?.TryGetValue(resourceKey, out value);
698+
699+
return value;
700+
}
701+
702+
/// <summary>
703+
/// Provides a WPF compatible version of TryFindResource to provide a static resource lookup.
704+
/// If the key is not found in the current element's resources, the logical tree is then
705+
/// searched element-by-element to look for the resource in each element's resources.
706+
/// If none of the elements contain the resource, the Application's resources are then searched.
707+
/// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/>.</para>
708+
/// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>.</para>
709+
/// </summary>
710+
/// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
711+
/// <param name="resourceKey">The resource key to search for.</param>
712+
/// <param name="value">The resulting value, if present.</param>
713+
/// <returns>Whether or not a value with the specified key has been found.</returns>
714+
public static bool TryFindResource(this FrameworkElement element, object resourceKey, out object? value)
715+
{
716+
return (value = TryFindResource(element, resourceKey)) is not null;
699717
}
700718
}
701719
}

0 commit comments

Comments
 (0)