-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Mhawker/ttb updates #3319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mhawker/ttb updates #3319
Changes from all commits
dc0d231
d68b119
3cb6f23
b0cf735
051856c
f7737e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -286,5 +286,37 @@ private static string ContentPropertySearch(Type type) | |
|
|
||
| return ContentPropertySearch(type.GetTypeInfo().BaseType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Provides a WPF compatible version of TryFindResource to provide a static resource lookup. | ||
| /// If the key is not found in the current element's resources, the logical tree is then searched element-by-element to look for the resource in each element's resources. | ||
| /// If none of the elements contain the resource, the Application's resources are then searched. | ||
| /// <seealso href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.tryfindresource"/> | ||
| /// <seealso href="https://docs.microsoft.com/en-us/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/> | ||
| /// </summary> | ||
| /// <param name="start"><see cref="FrameworkElement"/> to start searching for Resource.</param> | ||
| /// <param name="resourceKey">Key to search for.</param> | ||
| /// <returns>Requested resource or null.</returns> | ||
| public static object TryFindResource(this FrameworkElement start, object resourceKey) | ||
michael-hawker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| object value = null; | ||
| var current = start; | ||
|
|
||
| // Look in our dictionary and then walk-up parents | ||
| while (current != null) | ||
| { | ||
| if (current.Resources?.TryGetValue(resourceKey, out value) == true) | ||
| { | ||
| return value; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not keen on multiple return points from a function as it can make maintenance more tricky. |
||
| } | ||
|
|
||
| current = current.Parent as FrameworkElement; | ||
| } | ||
|
|
||
| // Finally try application resources. | ||
| Application.Current?.Resources?.TryGetValue(resourceKey, out value); | ||
|
|
||
| return value; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Toolkit.Diagnostics; | ||
| using Windows.UI.Xaml.Media; | ||
|
|
||
| namespace Microsoft.Toolkit.Uwp.UI.Helpers | ||
| { | ||
| /// <summary> | ||
| /// Provides helpers for the <see cref="CompositionTarget"/> class. | ||
| /// </summary> | ||
| public static class CompositionTargetHelper | ||
| { | ||
| /// <summary> | ||
| /// Provides a method to execute code after the rendering pass is completed. | ||
| /// <seealso href="https://github.com/microsoft/microsoft-ui-xaml/blob/c045cde57c5c754683d674634a0baccda34d58c4/dev/dll/SharedHelpers.cpp#L399"/> | ||
| /// </summary> | ||
| /// <param name="action">Action to be executed after render pass</param> | ||
| /// <returns>Awaitable Task</returns> | ||
| public static Task<bool> ExecuteAfterCompositionRenderingAsync(Action action) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like a Task based version (for the action parameter, Func) so the returned task from this method can await for the action to executed. Not sure its part of this PR, just something I noticed when I was reviewing this. |
||
| { | ||
| Guard.IsNotNull(action, nameof(action)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could/should this just be a no-op (Task.CompletedTask) in this case? Do we want to throw? |
||
|
|
||
| var taskCompletionSource = new TaskCompletionSource<bool>(); | ||
|
|
||
| try | ||
| { | ||
| void Callback(object sender, object args) | ||
| { | ||
| CompositionTarget.Rendering -= Callback; | ||
|
|
||
| action(); | ||
|
|
||
| taskCompletionSource.SetResult(true); | ||
| } | ||
|
|
||
| CompositionTarget.Rendering += Callback; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| taskCompletionSource.SetException(e); // Note this can just sometimes be a wrong thread exception, see WinUI function notes. | ||
| } | ||
|
|
||
| return taskCompletionSource.Task; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!