@@ -11,8 +11,6 @@ namespace Microsoft.AspNetCore.Components
11
11
/// </summary>
12
12
public abstract class NavigationManager
13
13
{
14
- private bool _hasLocationChangingListeners ;
15
-
16
14
/// <summary>
17
15
/// An event that fires when the navigation location has changed.
18
16
/// </summary>
@@ -41,18 +39,20 @@ public event EventHandler<LocationChangingEventArgs> LocationChanging
41
39
{
42
40
AssertInitialized ( ) ;
43
41
_locationChanging += value ;
44
- UpdateHasLocationChangingListeners ( true ) ;
42
+ UpdateHasLocationChangingEventHandlers ( ) ;
45
43
}
46
44
remove
47
45
{
48
46
AssertInitialized ( ) ;
49
47
_locationChanging -= value ;
50
- UpdateHasLocationChangingListeners ( _locationChanging != null ) ;
48
+ UpdateHasLocationChangingEventHandlers ( ) ;
51
49
}
52
50
}
53
51
54
52
private EventHandler < LocationChangingEventArgs > ? _locationChanging ;
55
53
54
+ private bool _hasLocationChangingEventHandlers ;
55
+
56
56
// For the baseUri it's worth storing as a System.Uri so we can do operations
57
57
// on that type. System.Uri gives us access to the original string anyway.
58
58
private Uri ? _baseUri ;
@@ -255,17 +255,28 @@ protected bool NotifyLocationChanging(string uri, bool isInterceptedLink)
255
255
/// Called when <see cref="LocationChanging"/> the fact that any event handlers are present or not changes.
256
256
/// this can be used by descendants to inform the JSRuntime that there are locationchanging event handlers
257
257
/// </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 )
260
261
{
262
+ return true ;
261
263
}
262
264
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 ( )
264
270
{
265
- if ( _hasLocationChangingListeners != value )
271
+ var value = _locationChanging != null ;
272
+ if ( _hasLocationChangingEventHandlers != value )
266
273
{
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
+ }
269
280
}
270
281
}
271
282
0 commit comments