Skip to content

Commit c37bd7b

Browse files
author
msftbot[bot]
authored
[Visual|Logical]Tree extensions revamp (#3685)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 📝 It is preferred if you keep the "☑️ Allow edits by maintainers" checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ## Closes #3487 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Feature <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## Overview <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> There are some inconsistencies in the visual tree extensions mentioned in the linked issue, and some missing features. This PR applies the changes mentioned in #3487 (comment). ## APIs breakdown <details> <summary><b>VisualTree (click to expand):</b></summary> <br/> ```csharp namespace Microsoft.Toolkit.Uwp.UI.Extensions { public static class VisualTree { static FrameworkElement? FindDescendant(this DependencyObject element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindDescendant<T>(this DependencyObject element) where T : notnull, DependencyObject; static DependencyObject? FindDescendant(this DependencyObject element, Type type); static T? FindDescendant<T>(this DependencyObject element, Func<T, bool> predicate) where T : notnull, DependencyObject; static T? FindDescendant<T, TState>(this DependencyObject element, TState state, Func<T, TState, bool> predicate) where T : notnull, DependencyObject; static FrameworkElement? FindDescendantOrSelf(this DependencyObject element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindDescendantOrSelf<T>(this DependencyObject element) where T : notnull, DependencyObject; static DependencyObject? FindDescendantOrSelf(this DependencyObject element, Type type); static T? FindDescendantOrSelf<T>(this DependencyObject element, Func<T, bool> predicate) where T : notnull, DependencyObject; static T? FindDescendantOrSelf<T, TState>(this DependencyObject element, TState state, Func<T, TState, bool> predicate) where T : notnull, DependencyObject; static IEnumerable<DependencyObject> FindDescendants(this DependencyObject element); static FrameworkElement? FindAscendant(this DependencyObject element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindAscendant<T>(this DependencyObject element) where T : notnull, DependencyObject; static DependencyObject? FindAscendant(this DependencyObject element, Type type); static T? FindAscendant<T>(this DependencyObject element, Func<T, bool> predicate) where T : notnull, DependencyObject; static T? FindAscendant<T, TState>(this DependencyObject element, TState state, Func<T, TState, bool> predicate) where T : notnull, DependencyObject; static FrameworkElement? FindAscendantOrSelf(this DependencyObject element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindAscendantOrSelf<T>(this DependencyObject element) where T : notnull, DependencyObject; static DependencyObject? FindAscendantOrSelf(this DependencyObject element, Type type); static T? FindAscendantOrSelf<T>(this DependencyObject element, Func<T, bool> predicate) where T : notnull, DependencyObject; static T? FindAscendantOrSelf<T, TState>(this DependencyObject element, TState state, Func<T, TState, bool> predicate) where T : notnull, DependencyObject; static IEnumerable<DependencyObject> FindAscendants(this DependencyObject element); } } ``` </details> <details> <summary><b>LogicalTree (click to expand):</b></summary> <br/> ```csharp namespace Microsoft.Toolkit.Uwp.UI.Extensions { public static class LogicalTree { static FrameworkElement? FindChild(this FrameworkElement element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindChild<T>(this FrameworkElement element) where T : notnull, FrameworkElement; static FrameworkElement? FindChild(this FrameworkElement element, Type type); static T? FindChild<T>(this FrameworkElement element, Func<T, bool> predicate) where T : notnull, FrameworkElement; static T? FindChild<T, TState>(this FrameworkElement element, TState state, Func<T, TState, bool> predicate) where T : notnull, FrameworkElement; static FrameworkElement? FindChildOrSelf(this FrameworkElement element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindChildOrSelf<T>(this FrameworkElement element) where T : notnull, FrameworkElement; static FrameworkElement? FindChildOrSelf(this FrameworkElement element, Type type); static T? FindChildOrSelf<T>(this FrameworkElement element, Func<T, bool> predicate) where T : notnull, FrameworkElement; static T? FindChildOrSelf<T, TState>(this FrameworkElement element, TState state, Func<T, TState, bool> predicate) where T : notnull, FrameworkElement; static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement element); static FrameworkElement? FindParent(this FrameworkElement element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindParent<T>(this FrameworkElement element) where T : notnull, FrameworkElement; static FrameworkElement? FindParent(this FrameworkElement element, Type type); static T? FindParent<T>(this FrameworkElement element, Func<T, bool> predicate) where T : notnull, FrameworkElement; static T? FindParent<T, TState>(this FrameworkElement element, TState state, Func<T, TState, bool> predicate) where T : notnull, FrameworkElement; static FrameworkElement? FindParentOrSelf(this FrameworkElement element, string name, StringComparison comparisonType = StringComparison.Ordinal); static T? FindParentOrSelf<T>(this FrameworkElement element) where T : notnull, FrameworkElement; static FrameworkElement? FindParentOrSelf(this FrameworkElement element, Type type); static T? FindParentOrSelf<T>(this FrameworkElement element, Func<T, bool> predicate) where T : notnull, FrameworkElement; static T? FindParentOrSelf<T, TState>(this FrameworkElement element, TState state, Func<T, TState, bool> predicate) where T : notnull, FrameworkElement; static IEnumerable<FrameworkElement> FindParents(this FrameworkElement element); static UIElement? TryGetContentControl(this FrameworkElement element); static object? TryFindResource(this FrameworkElement element, object resourceKey); } } ``` </details> ## PR Checklist Please check if your PR fulfills the following requirements: - [ ] Tested code with current [supported SDKs](../readme.md#supported) - [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [ ] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) - [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [ ] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [ ] Contains **NO** breaking changes <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. --> ## Other information Opening as draft, missing updated/revamped unit tests.
2 parents 221d1ec + 13b972a commit c37bd7b

File tree

63 files changed

+1248
-417
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1248
-417
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Pages/SampleController.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ private void UpdateXamlRender(string text)
517517

518518
if (CurrentSample.HasType)
519519
{
520-
root = SamplePage?.FindDescendantByName("XamlRoot");
520+
root = SamplePage?.FindDescendant("XamlRoot");
521521

522522
if (root is Panel)
523523
{

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/AdaptiveGridView/AdaptiveGridViewPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public AdaptiveGridViewPage()
2727

2828
public async void OnXamlRendered(FrameworkElement control)
2929
{
30-
_adaptiveGridViewControl = control.FindDescendantByName("AdaptiveGridViewControl") as AdaptiveGridView;
30+
_adaptiveGridViewControl = control.FindDescendant("AdaptiveGridViewControl") as AdaptiveGridView;
3131
if (_adaptiveGridViewControl != null)
3232
{
3333
var allPhotos = await new Data.PhotosDataSource().GetItemsAsync();

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/BladeView/BladePage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public BladePage()
2525

2626
public void OnXamlRendered(FrameworkElement control)
2727
{
28-
bladeView = control.FindChildByName("BladeView") as BladeView;
29-
addBlade = control.FindChildByName("AddBlade") as Button;
28+
bladeView = control.FindChild("BladeView") as BladeView;
29+
addBlade = control.FindChild("AddBlade") as Button;
3030

3131
if (addBlade != null)
3232
{

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/CameraPreview/CameraPreviewPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public async void OnXamlRendered(FrameworkElement control)
5454
_cameraPreviewControl.CameraHelper.FrameArrived += CameraPreviewControl_FrameArrived;
5555
}
5656

57-
_imageControl = control.FindDescendantByName("CurrentFrameImage") as Image;
57+
_imageControl = control.FindDescendant("CurrentFrameImage") as Image;
5858
if (_imageControl != null)
5959
{
6060
_softwareBitmapSource = new SoftwareBitmapSource();
6161
_imageControl.Source = _softwareBitmapSource;
6262
}
6363

64-
_errorMessageText = control.FindDescendantByName("ErrorMessage") as TextBlock;
64+
_errorMessageText = control.FindDescendant("ErrorMessage") as TextBlock;
6565

6666
semaphoreSlim.Release();
6767
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Carousel/CarouselPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public CarouselPage()
2929

3030
public async void OnXamlRendered(FrameworkElement control)
3131
{
32-
carouselControl = control.FindDescendantByName("CarouselControl") as Carousel;
32+
carouselControl = control.FindDescendant("CarouselControl") as Carousel;
3333
carouselControl.ItemsSource = await new Data.PhotosDataSource().GetItemsAsync();
3434
}
3535
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DataGrid/DataGridPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async void OnXamlRendered(FrameworkElement control)
3535
dataGrid.LoadingRowGroup -= DataGrid_LoadingRowGroup;
3636
}
3737

38-
dataGrid = control.FindDescendantByName("dataGrid") as DataGrid;
38+
dataGrid = control.FindDescendant("dataGrid") as DataGrid;
3939
if (dataGrid != null)
4040
{
4141
dataGrid.Sorting += DataGrid_Sorting;
@@ -54,7 +54,7 @@ public async void OnXamlRendered(FrameworkElement control)
5454
groupButton.Click -= GroupButton_Click;
5555
}
5656

57-
groupButton = control.FindDescendantByName("groupButton") as AppBarButton;
57+
groupButton = control.FindDescendant("groupButton") as AppBarButton;
5858
if (groupButton != null)
5959
{
6060
groupButton.Click += GroupButton_Click;

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DockPanel/DockPanelPage.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Windows.UI.Xaml;
1010
using Windows.UI.Xaml.Controls;
1111
using Windows.UI.Xaml.Media;
12-
using Windows.UI.Xaml.Navigation;
1312

1413
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
1514
{
@@ -29,7 +28,7 @@ public DockPanelPage()
2928

3029
public void OnXamlRendered(FrameworkElement control)
3130
{
32-
_sampleDockPanel = control.FindChildByName("SampleDockPanel") as DockPanel;
31+
_sampleDockPanel = control.FindChild("SampleDockPanel") as DockPanel;
3332
}
3433

3534
private void Load()

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/FadeHeader/FadeHeaderBehaviorPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public FadeHeaderBehaviorPage()
2626

2727
public void OnXamlRendered(FrameworkElement control)
2828
{
29-
myListView = control.FindChildByName("MyListView") as ListView;
29+
myListView = control.FindChild("MyListView") as ListView;
3030

3131
// Load the ListView with Sample Data
3232
if (myListView != null)

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/GazeInteraction/GazeInteractionPage.xaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using Microsoft.Toolkit.Uwp.Input.GazeInteraction;
76
using Microsoft.Toolkit.Uwp.UI.Extensions;
87
using Windows.UI.Core;
98
using Windows.UI.Xaml;
109
using Windows.UI.Xaml.Controls;
11-
using Windows.UI.Xaml.Shapes;
1210

1311
namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
1412
{
@@ -33,7 +31,7 @@ public void OnXamlRendered(FrameworkElement control)
3331

3432
WarnUserToPlugInDevice();
3533

36-
var buttonControl = control.FindChildByName("TargetButton") as Button;
34+
var buttonControl = control.FindChild("TargetButton") as Button;
3735

3836
if (buttonControl != null)
3937
{

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/GazeTracing/GazeTracingPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public GazeTracingPage()
4242

4343
public void OnXamlRendered(FrameworkElement control)
4444
{
45-
if (control.FindChildByName("Points") is ItemsControl itemsControl)
45+
if (control.FindChild("Points") is ItemsControl itemsControl)
4646
{
4747
itemsControl.ItemsSource = GazeHistory;
4848
}

0 commit comments

Comments
 (0)