Skip to content

Commit dce4311

Browse files
committed
Minor API improvements
Tweaked TryGetContentControl and TryFindResource, and added a throwing version of FindResource
1 parent 6d3b8b9 commit dce4311

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
using Microsoft.Toolkit.Uwp.UI.Extensions;
66
using Windows.Foundation;
77
using Windows.System;
8-
using Windows.UI.Core;
98
using Windows.UI.Xaml;
109
using Windows.UI.Xaml.Controls;
11-
using Windows.UI.Xaml.Controls.Primitives;
1210
using Windows.UI.Xaml.Data;
1311
using Windows.UI.Xaml.Input;
1412

@@ -107,7 +105,15 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
107105
{
108106
// This can be expensive, could we optimize?
109107
// Also, this is changing the FontSize on the IconSource (which could be shared?)
110-
fis.FontSize = Owner.TryFindResource("TokenizingTextBoxIconFontSize") as double? ?? 16;
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+
}
111117
}
112118

113119
var iconBinding = new Binding()

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static class LogicalTree
110110
}
111111
}
112112
}
113-
else if (element.TryGetContentControl() is FrameworkElement contentControl)
113+
else if (element.GetContentControl() is FrameworkElement contentControl)
114114
{
115115
if (contentControl is T result && predicate(result))
116116
{
@@ -179,7 +179,7 @@ public static class LogicalTree
179179
}
180180
}
181181
}
182-
else if (element.TryGetContentControl() is FrameworkElement contentControl)
182+
else if (element.GetContentControl() is FrameworkElement contentControl)
183183
{
184184
if (contentControl is T result && predicate(result, state))
185185
{
@@ -328,7 +328,7 @@ public static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement e
328328
}
329329
}
330330
}
331-
else if (element.TryGetContentControl() is FrameworkElement contentControl)
331+
else if (element.GetContentControl() is FrameworkElement contentControl)
332332
{
333333
yield return contentControl;
334334

@@ -560,11 +560,11 @@ public static IEnumerable<FrameworkElement> FindParents(this FrameworkElement el
560560
}
561561

562562
/// <summary>
563-
/// Tries to retrieve the content property of this element as defined by <see cref="ContentPropertyAttribute"/>.
563+
/// Gets the content property of this element as defined by <see cref="ContentPropertyAttribute"/>, if available.
564564
/// </summary>
565565
/// <param name="element">The parent element.</param>
566566
/// <returns>The retrieved content control, or <see langword="null"/> if not available.</returns>
567-
public static UIElement? TryGetContentControl(this FrameworkElement element)
567+
public static UIElement? GetContentControl(this FrameworkElement element)
568568
{
569569
Type type = element.GetType();
570570
TypeInfo? typeInfo = type.GetTypeInfo();
@@ -595,15 +595,41 @@ public static IEnumerable<FrameworkElement> FindParents(this FrameworkElement el
595595
/// If the key is not found in the current element's resources, the logical tree is then
596596
/// searched element-by-element to look for the resource in each element's resources.
597597
/// If none of the elements contain the resource, the Application's resources are then searched.
598-
/// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/></para>
599-
/// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/></para>
598+
/// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/>.</para>
599+
/// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>.</para>
600600
/// </summary>
601601
/// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
602602
/// <param name="resourceKey">The resource key to search for.</param>
603-
/// <returns>The requested resource, or <see langword="null"/>.</returns>
604-
public static object? TryFindResource(this FrameworkElement element, object resourceKey)
603+
/// <returns>The requested resource.</returns>
604+
/// <exception cref="Exception">Thrown when no resource is found with the specified key.</exception>
605+
public static object FindResource(this FrameworkElement element, object resourceKey)
605606
{
606-
object? value = null;
607+
if (TryFindResource(element, resourceKey, out object? value))
608+
{
609+
return value!;
610+
}
611+
612+
static object Throw() => throw new Exception("No resource was found with the specified key");
613+
614+
return Throw();
615+
}
616+
617+
/// <summary>
618+
/// Provides a WPF compatible version of TryFindResource to provide a static resource lookup.
619+
/// If the key is not found in the current element's resources, the logical tree is then
620+
/// searched element-by-element to look for the resource in each element's resources.
621+
/// If none of the elements contain the resource, the Application's resources are then searched.
622+
/// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/>.</para>
623+
/// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>.</para>
624+
/// </summary>
625+
/// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
626+
/// <param name="resourceKey">The resource key to search for.</param>
627+
/// <param name="value">The resulting value, if present.</param>
628+
/// <returns>Whether or not a value with the specified key has been found.</returns>
629+
public static bool TryFindResource(this FrameworkElement element, object resourceKey, out object? value)
630+
{
631+
value = null;
632+
607633
FrameworkElement? current = element;
608634

609635
// Look in our dictionary and then walk-up parents. We use a do-while loop here
@@ -613,17 +639,15 @@ public static IEnumerable<FrameworkElement> FindParents(this FrameworkElement el
613639
{
614640
if (current.Resources?.TryGetValue(resourceKey, out value) == true)
615641
{
616-
return value!;
642+
return true;
617643
}
618644

619645
current = current.Parent as FrameworkElement;
620646
}
621647
while (current is not null);
622648

623649
// Finally try application resources
624-
Application.Current?.Resources?.TryGetValue(resourceKey, out value);
625-
626-
return value;
650+
return Application.Current?.Resources?.TryGetValue(resourceKey, out value) == true;
627651
}
628652
}
629653
}

0 commit comments

Comments
 (0)