-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 for Application Crash When Updating ToolbarItem Icon and Navigating Between Shell Pages #26126
Conversation
Fix for toolbar issue
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
/rebase |
0328b1a
to
ce35de3
Compare
Azure Pipelines successfully started running 3 pipeline(s). |
Azure Pipelines successfully started running 3 pipeline(s). |
@@ -98,7 +98,7 @@ void OnPropertyChanged(object sender, PropertyChangedEventArgs e) | |||
|
|||
void UpdateIconAndStyle(ToolbarItem item) | |||
{ | |||
if (item?.IconImageSource == null) | |||
if (item?.IconImageSource == null || item.Parent?.Handler is null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to set the Image to null
if the Parent Handler
is null?
Maybe the check should be added to the else? Or the method should just exit out of MauiContext
is null?
var mc = item.FindMauiContext()
if (mc is null) return;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PureWeen Thank you for the feedback. I've implemented the changes as suggested, focusing on proper MauiContext handling. The method now checks for a valid context before proceeding with image loading, addressing the concerns about potential null references. Please review and let us know if any further changes are required.
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
|
Root Cause
OnPropertyChanged
event for theToolbarItem.IconImageSource
was triggered before the page's handler was created during the second navigation. Initially, on the first navigation, this event fired after the page handler was initialized, allowing theUpdateIconAndStyle
method to access the MauiContext throughitem.FindMauiContext()
. However, after updating theToolbarItem.IconImageSource
multiple times, the event sequence changed. On subsequent navigations, theOnPropertyChanged
event fired before the page handler was created, leaving the ToolbarItem without a valid parent handler. As a result, attempts to accessitem.FindMauiContext()
failed, leading to a null reference exception and causing the app to crash.The sample code used an
.svg
file for the toolbar icon without the correct extension or appropriate handling. Per .NET MAUI guidelines, SVG files are automatically converted to PNG files and must be referenced with a.png
extension in the project. Using the incorrect extension or without extension caused the app to fail with error.Description of Change
To address this issue, a condition was added to the
UpdateIconAndStyle
method to ensure theToolbarItem
's parent handler exists before attempting to accessitem.FindMauiContext()
. This additional check prevents the method from executing when the required resources are not yet initialized, avoiding any null reference exceptions. This change ensures that updates toToolbarItem.IconImageSource
are safely handled during navigation, maintaining stability and preventing crashes caused by uninitialized platform dependencies.Issues Fixed
Fixes #25534
Tested the behaviour in the following platforms
Output Video
withoutfix.mov
withfix.mov