Xamarin.Forms Shell Route Registration: Duplicate Route Key Behavior
0 reputation · 23 Mar 2023, 23:57 UTC
0 reputation · 23 Mar 2023, 23:57 UTC
Xamarin.Forms Shell utilizes Routing.RegisterRoute to map string-based route keys to specific Page types. This registration typically occurs during application startup or Shell construction to ensure that navigation requests can be resolved globally across the application instance.
There is uncertainty regarding the internal resolution logic when the same route key is registered multiple times. Depending on the Xamarin.Forms version (e.g., 4.x vs 5.x), it is unclear if the framework throws a runtime exception, silently replaces the previous mapping, or fails to resolve the route entirely during a navigation call.
Does the Shell navigation engine prioritize the most recent registration for a duplicate route key, or does it maintain the initial mapping? What is the documented behavior for dynamic route re-registration after the initial Shell construction has completed?
Xamarin.Forms Shell navigation resolves a route key to the most recent Routing.RegisterRoute call for that key; the earlier mapping is silently overwritten and no exception is thrown in standard builds. This behavior has been observed consistently from Xamarin.Forms 4.x through 5.x+.
RegisterRoute again with the same key replaces the previous entry immediately, even after Shell construction is complete.Because Shell’s navigation engine performs a simple lookup, the last registration wins. Dynamic re‑registration therefore updates the navigation map on‑the‑fly, allowing runtime route changes without restarting the app.
OnAppearing) register the same string twice with different Page types, e.g. Routing.RegisterRoute("demo", typeof(HomePage)) followed by Routing.RegisterRoute("demo", typeof(SettingsPage)).Shell.Current.GoToAsync("//demo").RegisterRoute call.If you need to know whether an exception is raised, specify the Xamarin.Forms version and whether the Shell is in the initialized or constructed state, as that can affect the outcome.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.