fix(Navigation): Use NullLogger fallback with diagnostic warning and add documentation to prevent crash when Region.Attached is used in ExtendedSplashScreen - #2958
Conversation
…enceException when Region.Attached is used before NavigationHostedService starts Co-authored-by: kazo0 <4793020+kazo0@users.noreply.github.com>
|
|
|
@copilot This change doesn’t actually make the exception more descriptive, it removes the exception entirely. We should provide a better warning/error message about proper usage. By returning What I was expecting was either:
Right now, if something goes wrong later in the navigation pipeline, there won’t be any indication that the logger was never set. |
…lization Co-authored-by: vatsashah45 <72754751+vatsashah45@users.noreply.github.com>
Co-authored-by: vatsashah45 <72754751+vatsashah45@users.noreply.github.com>
You're absolutely right. I've updated the fix to provide a clear diagnostic warning message when the logger isn't initialized (commit c4d3e26). The solution now:
This gives developers visibility into what's happening while still allowing the application to run gracefully. |
|
@copilot I was expecting you to get to the root of the problem as to why the exception is being thrown at the point of the runtime. To me, it seems to be a valid use-case to have a Grid with Region.Attached="True" as part of the Content of the ExtendedSplashScreen in the Shell. Upon inspection of this runtime exception and asking copilot why it was happening I was told: The problem is that when the XAML is parsed and Region.Attached="True" is encountered, it tries to initialize the navigation region, which needs the DI container to be ready - but it's not ready yet during the initial Shell construction. With that information, can you find a better fix or a better way to log and document this usage as an error? |
…d region initialization Co-authored-by: kazo0 <4793020+kazo0@users.noreply.github.com>
After investigating the initialization flow, I found that the navigation system already properly defers region initialization. When
The only issue was that Using
Updated the diagnostic message to use |
|
@copilot We need to make sure that, when using LLMs to generate our app code, it does not add content to the Shell.xaml that is using Region.Attached. We should be adding something to the Navigation markdown documentation to mention avoiding this. the Uno Docs MCP should be able to provide that info to the LLM/agent |
…content Co-authored-by: kazo0 <4793020+kazo0@users.noreply.github.com>
Added documentation to
This documentation will be available to the Uno Docs MCP and LLM agents (commit 55bf394). |
|
@copilot You should add a concise version of these new documentation changes to the Walkthrough article located here: Navigation/Walkthrough/DefineRegions.md |
Co-authored-by: kazo0 <4793020+kazo0@users.noreply.github.com>
Added a concise IMPORTANT callout to |
|
|
|
@Mergifyio backport release/stable/7.0 |
✅ Backports have been createdDetails
|
….0/pr-2958 fix(Navigation): Use NullLogger fallback with diagnostic warning and add documentation to prevent crash when Region.Attached is used in ExtendedSplashScreen (backport #2958)
GitHub Issue (If applicable): closes #2957
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Using
Region.Attached="True"inside anExtendedSplashScreencontent throwsNullReferenceException: "Logger needs to be set"because XAML parsing triggersAttachedChangedbeforeNavigationHostedService.StartAsyncinitializes the logger.What is the new behavior?
The navigation system already properly defers region initialization until services are available. When
Region.Attached="True"is encountered:NavigationRegionis created and hooks upViewLoading/ViewLoadedeventsAssignParentis called which looks for the service providerThe only missing piece was that
Region.Loggerwas accessed beforeNavigationHostedService.StartAsyncran, causing the crash. This fix:NullLogger<NavigationRegion>.Instanceas a fallback when the logger hasn't been set yetDebug.WriteLineexplaining the deferred initialization behaviorInterlocked.CompareExchangeto ensure the message only appears onceCode Changes:
Documentation Changes:
doc/Learn/Navigation/HowTo-Regions.mdthat explains whyRegion.Attached="True"should not be used insideExtendedSplashScreencontent, shows incorrect usage with a code example marked to avoid, and provides the correct approach with examples showing where to define regions instead.doc/Learn/Navigation/Walkthrough/DefineRegions.mdwarning developers not to useRegion.Attached="True"inside Shell.xaml or ExtendedSplashScreen content.This documentation will be available to the Uno Docs MCP and LLM agents to prevent them from generating code with this anti-pattern.
PR Checklist
Please check if your PR fulfills the following requirements:
Screenshots Compare Test Runresults.Other information
The documentation added to both
HowTo-Regions.mdandWalkthrough/DefineRegions.mdserves as guidance for both developers and LLM/AI agents to avoid usingRegion.Attachedin Shell.xaml content, which is an anti-pattern that can cause initialization issues.Internal Issue (If applicable):
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.