@@ -11,8 +11,6 @@ namespace Microsoft.AspNetCore.Components
1111 /// </summary>
1212 public abstract class NavigationManager
1313 {
14- private bool _hasLocationChangingListeners ;
15-
1614 /// <summary>
1715 /// An event that fires when the navigation location has changed.
1816 /// </summary>
@@ -41,18 +39,20 @@ public event EventHandler<LocationChangingEventArgs> LocationChanging
4139 {
4240 AssertInitialized ( ) ;
4341 _locationChanging += value ;
44- UpdateHasLocationChangingListeners ( true ) ;
42+ UpdateHasLocationChangingEventHandlers ( ) ;
4543 }
4644 remove
4745 {
4846 AssertInitialized ( ) ;
4947 _locationChanging -= value ;
50- UpdateHasLocationChangingListeners ( _locationChanging != null ) ;
48+ UpdateHasLocationChangingEventHandlers ( ) ;
5149 }
5250 }
5351
5452 private EventHandler < LocationChangingEventArgs > ? _locationChanging ;
5553
54+ private bool _hasLocationChangingEventHandlers ;
55+
5656 // For the baseUri it's worth storing as a System.Uri so we can do operations
5757 // on that type. System.Uri gives us access to the original string anyway.
5858 private Uri ? _baseUri ;
@@ -255,17 +255,28 @@ protected bool NotifyLocationChanging(string uri, bool isInterceptedLink)
255255 /// Called when <see cref="LocationChanging"/> the fact that any event handlers are present or not changes.
256256 /// this can be used by descendants to inform the JSRuntime that there are locationchanging event handlers
257257 /// </summary>
258- /// <param name="value">true if there are eventhandlers</param>
259- protected virtual void SetHasLocationChangingListeners ( bool value )
258+ /// <param name="value">true if there are event handlers</param>
259+ /// <returns>true when the navigation subsystem could be informed that we have event handlers</returns>
260+ protected virtual bool SetHasLocationChangingEventHandlers ( bool value )
260261 {
262+ return true ;
261263 }
262264
263- private void UpdateHasLocationChangingListeners ( bool value )
265+ /// <summary>
266+ /// Calls <see cref="SetHasLocationChangingEventHandlers"/> when needed
267+ /// This function is normally called when event handlers are added or removed from <see cref="LocationChanging"/>
268+ /// </summary>
269+ protected void UpdateHasLocationChangingEventHandlers ( )
264270 {
265- if ( _hasLocationChangingListeners != value )
271+ var value = _locationChanging != null ;
272+ if ( _hasLocationChangingEventHandlers != value )
266273 {
267- _hasLocationChangingListeners = value ;
268- SetHasLocationChangingListeners ( value ) ;
274+ //If SetHasLocationChangingEventHandlers returns false, we won't update the _hasLocationChangingEventHandlers.
275+ //This way we can call this function again at a later time (for example when JSRuntime is initialized, See RemoteNavigationManager)
276+ if ( SetHasLocationChangingEventHandlers ( value ) )
277+ {
278+ _hasLocationChangingEventHandlers = value ;
279+ }
269280 }
270281 }
271282
0 commit comments