From 03959e785f635697fcdf0f99aad9454fafbf4e2e Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 14 Jun 2018 11:57:22 +0300 Subject: Improved UdpDiscoveryClient (I Think...). Improved PPC NavigationManager by providing a way to navigate by module/view paths.. --- .../ExtensionMethods/DependencyObjectExtensions.cs | 2 + .../ExtensionMethods/FrameworkElementExtensions.cs | 133 +++++++++++++++++++++ 2 files changed, 135 insertions(+) (limited to 'Software/Visual_Studio/Tango.Core/ExtensionMethods') diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs index a12ca4f7f..12eba6bb3 100644 --- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs +++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/DependencyObjectExtensions.cs @@ -2,9 +2,11 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; +using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Animation; diff --git a/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs b/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs index 216f6ae0c..2193127cf 100644 --- a/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs +++ b/Software/Visual_Studio/Tango.Core/ExtensionMethods/FrameworkElementExtensions.cs @@ -1,11 +1,14 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; +using System.Windows.Markup; using Tango.Core.EventArguments; public static class FrameworkElementExtensions @@ -309,5 +312,135 @@ public static class FrameworkElementExtensions #endregion + #region Loaded + /// + /// Registers for the element loaded event. if the element is already loaded will invoke the handler immediately. + /// + /// The element. + /// The handler. + public static void RegisterForLoadedOrNow(this FrameworkElement element, EventHandler handler) + { + if (element.IsLoaded) + { + handler(element, new EventArgs()); + } + else + { + bool isLoaded = false; + + element.Loaded += (sender, e) => + { + if (!isLoaded) + { + isLoaded = true; + + handler(element, new EventArgs()); + } + }; + } + } + + #endregion + + /// + /// A recursive search for a child of type T in memory and not on logical/visual tree. + /// + /// + /// The element. + /// + public static T FindChildOffline(this FrameworkElement element) where T : DependencyObject + { + if (element == null) return null; + + PropertyInfo contentProp = element.GetType().GetProperty("Child"); + + if (contentProp == null) + { + contentProp = element.GetType().GetProperty("Content"); + } + + if (contentProp != null) + { + var value = contentProp.GetValue(element); + + if (value != null) + { + if (value.GetType() == typeof(T)) + { + return (T)value; + } + else + { + return FindChildOffline(value as FrameworkElement); + } + } + } + + if (element is Panel) + { + foreach (var child in (element as Panel).Children.OfType()) + { + if (child.GetType() == typeof(T)) + { + return child as T; + } + else + { + var result = FindChildOffline(child); + + if (result != null) + { + return result; + } + } + } + } + + var contentAtt = element.GetType().GetCustomAttribute(); + + if (contentAtt != null) + { + contentProp = element.GetType().GetProperty(contentAtt.Name); + + if (contentProp != null) + { + var content = contentProp.GetValue(element); + + if (content != null) + { + if (content is IEnumerable) + { + foreach (var child in (content as IEnumerable)) + { + if (child.GetType() == typeof(T)) + { + return child as T; + } + else + { + var result = FindChildOffline(child as FrameworkElement); + + if (result != null) + { + return result; + } + } + } + } + else + { + var result = FindChildOffline(content as FrameworkElement); + + if (result != null) + { + return result; + } + } + } + } + } + + return null; + } } -- cgit v1.3.1