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

fix: Fix possible null ref #19411

Merged
merged 2 commits into from
Jan 31, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ select handler.Value
// For instance, a DataTemplate in a resource dictionary may mark the type as updated in `updatedTypes`
// but it will not be considered as a new type even if "CreateNewOnMetadataUpdate" was set.

return (fe, ImmutableArray<ElementUpdateHandlerActions>.Empty, liveType);
return (fe, [], liveType);
}
else
{
return !handlers.IsDefaultOrEmpty || mappedType is not null
return (!handlers.IsDefaultOrEmpty || mappedType is not null)
? (fe, handlers, mappedType)
: default;
: (null, [], null);
}
},
parentKey: default);
Expand All @@ -208,21 +208,26 @@ select handler.Value
// or replace the element with a new one
foreach (var (element, elementHandlers, elementMappedType) in instancesToUpdate)
{
if (element is null)
{
continue;
}

// Action: ElementUpdate
// This is invoked for each existing element that is in the tree that needs to be replaced
foreach (var elementHandler in elementHandlers)
{
elementHandler?.ElementUpdate(element, updatedTypes);
}

if (elementMappedType is not null)
if (elementMappedType is not null)
{
if (_log.IsEnabled(LogLevel.Trace))
{
if (_log.IsEnabled(LogLevel.Trace))
{
_log.Error($"Updating element [{element}] to [{elementMappedType}]");
}

ReplaceViewInstance(element, elementMappedType, elementHandler);
_log.Error($"Updating element [{element}] to [{elementMappedType}]");
}

ReplaceViewInstance(element, elementMappedType, elementHandlers, updatedTypes);
}
}

Expand Down Expand Up @@ -408,7 +413,7 @@ private static void UpdateResourceDictionaries(List<Uri> updatedDictionaries, Re
}
#endif

private static void ReplaceViewInstance(UIElement instance, Type replacementType, ElementUpdateAgent.ElementUpdateHandlerActions? handler = default, Type[]? updatedTypes = default)
private static void ReplaceViewInstance(UIElement instance, Type replacementType, in ImmutableArray<ElementUpdateHandlerActions> handlers, Type[] updatedTypes)
{
if (replacementType.GetConstructor(Array.Empty<Type>()) is { } creator)
{
Expand All @@ -429,11 +434,17 @@ private static void ReplaceViewInstance(UIElement instance, Type replacementType
oldStore.ClonePropertiesToAnotherStoreForHotReload(newStore);
#endif

handler?.BeforeElementReplaced(instanceFE, newInstanceFE, updatedTypes);
foreach (var handler in handlers)
{
handler.BeforeElementReplaced(instanceFE, newInstanceFE, updatedTypes);
}

SwapViews(instanceFE, newInstanceFE);

handler?.AfterElementReplaced(instanceFE, newInstanceFE, updatedTypes);
foreach (var handler in handlers)
{
handler.AfterElementReplaced(instanceFE, newInstanceFE, updatedTypes);
}
}
}
else
Expand Down
Loading