|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Text.Json.Serialization; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.Toolkit; |
| 12 | +using Microsoft.Toolkit.Uwp; |
| 13 | +using Microsoft.Toolkit.Uwp.UI; |
| 14 | +using UITests.App.Pages; |
| 15 | +using Windows.Foundation.Collections; |
| 16 | +using Windows.System; |
| 17 | +using Windows.UI.Xaml; |
| 18 | +using Windows.UI.Xaml.Controls; |
| 19 | + |
| 20 | +namespace UITests.App.Commands |
| 21 | +{ |
| 22 | + public static class VisualTreeHelperCommands |
| 23 | + { |
| 24 | + private static DispatcherQueue Queue { get; set; } |
| 25 | + |
| 26 | + private static JsonSerializerOptions SerializerOptions { get; } = new JsonSerializerOptions(JsonSerializerDefaults.General) |
| 27 | + { |
| 28 | + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, |
| 29 | + }; |
| 30 | + |
| 31 | + public static void Initialize(DispatcherQueue uiThread) |
| 32 | + { |
| 33 | + Queue = uiThread; |
| 34 | + |
| 35 | + (App.Current as App).RegisterCustomCommand("VisualTreeHelper.FindElementProperty", FindElementProperty); |
| 36 | + } |
| 37 | + |
| 38 | + public static async Task<ValueSet> FindElementProperty(ValueSet arguments) |
| 39 | + { |
| 40 | + ValueSet results = new ValueSet(); |
| 41 | + |
| 42 | + if (Queue == null) |
| 43 | + { |
| 44 | + Log.Error("VisualTreeHelper - Missing UI DispatcherQueue"); |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + await Queue.EnqueueAsync(() => |
| 49 | + { |
| 50 | + // Dispatch? |
| 51 | + var content = Window.Current.Content as Frame; |
| 52 | + |
| 53 | + if (content == null) |
| 54 | + { |
| 55 | + Log.Error("VisualTreeHelper.FindElementProperty - Window has no content."); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (arguments.TryGetValue("ElementName", out object value) && value is string name && |
| 60 | + arguments.TryGetValue("Property", out object value2) && value2 is string propertyName) |
| 61 | + { |
| 62 | + Log.Comment("VisualTreeHelper.FindElementProperty('{0}', '{1}')", name, propertyName); |
| 63 | + |
| 64 | + // 1. Find Element in Visual Tree |
| 65 | + var element = content.FindDescendant(name); |
| 66 | + |
| 67 | + try |
| 68 | + { |
| 69 | + Log.Comment("VisualTreeHelper.FindElementProperty - Found Element? {0}", element != null); |
| 70 | + |
| 71 | + var typeinfo = element.GetType().GetTypeInfo(); |
| 72 | + |
| 73 | + Log.Comment("Element Type: {0}", typeinfo.FullName); |
| 74 | + |
| 75 | + var prop = element.GetType().GetTypeInfo().GetProperty(propertyName); |
| 76 | + |
| 77 | + if (prop == null) |
| 78 | + { |
| 79 | + Log.Error("VisualTreeHelper.FindElementProperty - Couldn't find Property named {0} on type {1}", propertyName, typeinfo.FullName); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // 2. Get the property using reflection |
| 84 | + var propValue = prop.GetValue(element); |
| 85 | + |
| 86 | + // 3. Serialize and return the result |
| 87 | + results.Add("Result", JsonSerializer.Serialize(propValue, SerializerOptions)); |
| 88 | + } |
| 89 | + catch (Exception e) |
| 90 | + { |
| 91 | + Log.Error("Error {0}", e.Message); |
| 92 | + Log.Error("StackTrace:\n{0}", e.StackTrace); |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + if (results.Count > 0) |
| 98 | + { |
| 99 | + return results; |
| 100 | + } |
| 101 | + |
| 102 | + return null; // Failure |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments