Skip to content
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

Element: NRE by accessing RealParent - fix #23405

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/Controls/src/Core/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,28 @@ public Element Parent

void SetParent(Element value)
{
if (RealParent == value)
Element realParent = RealParent;

if (realParent == value)
{
return;
}

OnPropertyChanging(nameof(Parent));

if (_parentOverride == null)
{
OnParentChangingCore(Parent, value);
}

if (RealParent != null)
if (realParent is IElementDefinition element)
{
((IElementDefinition)RealParent).RemoveResourcesChangedListener(OnParentResourcesChanged);
element.RemoveResourcesChangedListener(OnParentResourcesChanged);

if (value != null && (RealParent is Layout || RealParent is IControlTemplated))
Application.Current?.FindMauiContext()?.CreateLogger<Element>()?.LogWarning($"{this} is already a child of {RealParent}. Remove {this} from {RealParent} before adding to {value}.");
if (value != null && (element is Layout || element is IControlTemplated))
{
Application.Current?.FindMauiContext()?.CreateLogger<Element>()?.LogWarning($"{this} is already a child of {element}. Remove {this} from {element} before adding to {value}.");
}
}

RealParent = value;
Expand All @@ -422,7 +430,9 @@ void SetParent(Element value)
OnParentSet();

if (_parentOverride == null)
{
OnParentChangedCore();
}

OnPropertyChanged(nameof(Parent));
}
Expand Down
Loading