-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For now there are internal methods we can call for adding/removing logical children without having to maintain a list ourselves. This moves the logic out into extension methods which currently uses reflection to get either the internal method, or if that's not found, on newer .NET 8 previews/rc's the method is public so we can safeguard to get that via reflection until a newer sdk is released where we can compile against the upcoming change.
- Loading branch information
Showing
3 changed files
with
113 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.Maui.Controls; | ||
|
||
internal static class ViewExtensions | ||
{ | ||
static PropertyInfo DataTemplateIdPropertyInfo; | ||
|
||
internal static string GetDataTemplateId(this DataTemplate dataTemplate) | ||
{ | ||
DataTemplateIdPropertyInfo ??= dataTemplate.GetType().GetProperty("Id", BindingFlags.Instance | BindingFlags.NonPublic); | ||
|
||
return DataTemplateIdPropertyInfo.GetValue(dataTemplate)?.ToString(); | ||
|
||
} | ||
|
||
static MethodInfo removeLogicalChildMethod = null; | ||
|
||
internal static void RemoveLogicalChild(this Element parent, IView view) | ||
{ | ||
if (view is Element elem) | ||
{ | ||
removeLogicalChildMethod ??= GetLogicalChildMethod(parent, "RemoveLogicalChildInternal", "RemoveLogicalChild"); | ||
removeLogicalChildMethod?.Invoke(parent, new[] { elem }); | ||
} | ||
} | ||
|
||
static MethodInfo addLogicalChildMethod = null; | ||
|
||
internal static void AddLogicalChild(this Element parent, IView view) | ||
{ | ||
if (view is Element elem) | ||
{ | ||
addLogicalChildMethod ??= GetLogicalChildMethod(parent, "AddLogicalChildInternal", "AddLogicalChild"); | ||
addLogicalChildMethod?.Invoke(parent, new[] { elem }); | ||
} | ||
} | ||
|
||
static MethodInfo GetLogicalChildMethod(Element parent, string internalName, string publicName) | ||
{ | ||
var internalMethod = parent.GetType().GetMethod( | ||
internalName, | ||
BindingFlags.Instance | BindingFlags.NonPublic, | ||
new[] { typeof(Element) }); | ||
|
||
if (internalMethod is null) | ||
{ | ||
internalMethod = parent.GetType().GetMethod( | ||
publicName, | ||
BindingFlags.Instance | BindingFlags.Public, | ||
new[] { typeof(Element) }); | ||
} | ||
|
||
return internalMethod; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters