Skip to content

Commit

Permalink
fix: Fix Xaml generator regression for x:Load trying to unload elemen…
Browse files Browse the repository at this point in the history
…ts in different scope
  • Loading branch information
Youssef1313 committed Aug 22, 2024
1 parent b2d4d18 commit 8f1a194
Showing 1 changed file with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5856,6 +5856,17 @@ private IEnumerable<XamlMemberDefinition> GetExtendedProperties(XamlObjectDefini
);
}

private static bool IsNewScope(XamlObjectDefinition xamlObjectDefinition)
{
var typeName = xamlObjectDefinition.Type.Name;
return typeName == "DataTemplate" ||
typeName == "ItemsPanelTemplate" ||
typeName == "ControlTemplate" ||
// This case is specific the custom ListView for iOS. Should be removed
// when the list rebuilt to be compatible.
typeName == "ListViewBaseLayoutTemplate";
}

private void BuildChild(IIndentedStringBuilder writer, XamlMemberDefinition? owner, XamlObjectDefinition xamlObjectDefinition, string? outerClosure = null)
{
_generatorContext.CancellationToken.ThrowIfCancellationRequested();
Expand All @@ -5881,17 +5892,7 @@ private void BuildChild(IIndentedStringBuilder writer, XamlMemberDefinition? own

using (TrySetDefaultBindMode(xamlObjectDefinition))
{
var isItemsPanelTemplate = typeName == "ItemsPanelTemplate";

if (
typeName == "DataTemplate"
|| isItemsPanelTemplate
|| typeName == "ControlTemplate"

// This case is specific the custom ListView for iOS. Should be removed
// when the list rebuilt to be compatible.
|| typeName == "ListViewBaseLayoutTemplate"
)
if (IsNewScope(xamlObjectDefinition))
{
writer.AppendIndented($"new {GetGlobalizedTypeName(fullTypeName)}(");

Expand Down Expand Up @@ -6160,7 +6161,7 @@ private void BuildNameCache(XamlObjectDefinition topLevelControl)
private List<string> FindNamesIn(XamlObjectDefinition xamlObjectDefinition)
{
var list = new List<string>();
foreach (var element in EnumerateSubElements(xamlObjectDefinition))
foreach (var element in EnumerateSubElements(xamlObjectDefinition, stoppingCondition: IsNewScope))
{
var nameMember = FindMember(element, "Name");

Expand Down Expand Up @@ -6212,34 +6213,44 @@ private List<string> FindNamesIn(XamlObjectDefinition xamlObjectDefinition)
/// <param name="xamlObject">The root from which to start the search</param>
/// <param name="elementName">The x:Name value to search for</param>
/// <returns></returns>
private IEnumerable<XamlObjectDefinition> EnumerateSubElements(XamlObjectDefinition xamlObject)
private IEnumerable<XamlObjectDefinition> EnumerateSubElements(XamlObjectDefinition xamlObject, Func<XamlObjectDefinition, bool>? stoppingCondition = null)
{
yield return xamlObject;

foreach (var member in xamlObject.Members)
{
foreach (var element in EnumerateSubElements(member.Objects))
foreach (var element in EnumerateSubElements(member.Objects, stoppingCondition))
{
yield return element;
}
}

var objects = xamlObject.Objects;

foreach (var element in EnumerateSubElements(objects))
foreach (var element in EnumerateSubElements(objects, stoppingCondition))
{
yield return element;
}
}

private IEnumerable<XamlObjectDefinition> EnumerateSubElements(IEnumerable<XamlObjectDefinition> objects)
private IEnumerable<XamlObjectDefinition> EnumerateSubElements(IEnumerable<XamlObjectDefinition> objects, Func<XamlObjectDefinition, bool>? stoppingCondition)
{
foreach (var child in objects.Safe())
{
if (stoppingCondition != null && stoppingCondition(child))
{
continue;
}

yield return child;

foreach (var innerElement in EnumerateSubElements(child))
foreach (var innerElement in EnumerateSubElements(child, stoppingCondition))
{
if (stoppingCondition != null && stoppingCondition(innerElement))
{
continue;
}

yield return innerElement;
}
}
Expand Down

0 comments on commit 8f1a194

Please sign in to comment.