当前位置:网站首页>Blazor University (27) routing - detect navigation events
Blazor University (27) routing - detect navigation events
2022-06-09 10:21:00 【Dotnet cross platform】
Link to the original text :https://blazor-university.com/routing/detecting-navigation-events/
Detect navigation events
Source code [1]
from Blazor Access to browser navigation is through NavigationManager service-provided . This can be used razor In the document @inject or CS In the document [Inject] Attribute injection into Blazor In the component .
LocationChanged event
LocationChanged Is in the browser URL Event triggered when a change occurs . It passes a LocationChangedEventArgs example , This example provides the following information :
public readonly struct LocationChangedEventArgs
{
public string Location { get; }
public bool IsNavigationIntercepted { get; }
}Location Property is the complete... That is displayed in the browser URL, Including the agreement 、 Path and any query string .
IsNavigationIntercepted Indicates whether navigation is through code or through HTML Navigation is activated .
falseNavigation is called from code
NavigationManager.NavigateTostart-up .trueThe user clicks HTML Navigation elements ( for example
a href),Blazor Intercept navigation , Instead of allowing the browser to actually navigate to the new URL, This will result in a request to the server . The same is true in other cases , For example, some on the page JavaScript Cause navigation ( for example , After timeout ). Final , Anything that is not throughNavigationManager.NavigateToAll navigation events initiated will be regarded as intercepting navigation , And the value istrue.
Please note that , Navigation cannot be blocked and prevented from continuing at this time .
Observe OnLocationChanged event
It should be noted that ,NavigationManager Service is a long-standing instance . therefore , Any subscription to its LocationChanged The components of events will be strongly referenced throughout the service life cycle . therefore , It is important that our components unsubscribe from this event when they are destroyed , Otherwise they will not be garbage collected .
at present ,ComponentBase Class has no lifecycle events when it is destroyed , But it can be done IDisposable Interface .
@implement IDisposable
@inject NavigationManager NavigationManager
protected override void OnInitialized()
{
// Subscribe to the event
NavigationManager.LocationChanged += LocationChanged;
base.OnInitialized();
}
void LocationChanged(object sender, LocationChangedEventArgs e)
{
string navigationMethod = e.IsNavigationIntercepted ? "HTML" : "code";
System.Diagnostics.Debug.WriteLine($"Notified of navigation via {navigationMethod} to {e.Location}");
}
void IDisposable.Dispose()
{
// Unsubscribe from the event when our component is disposed
NavigationManager.LocationChanged -= LocationChanged;
}Reference material
[1]
Source code : https://github.com/mrpmorris/blazor-university/tree/master/src/Routing/NavigatingViaCode
边栏推荐
- Machine learning notes - Interpretation of u-net papers
- 什么样的数字藏品平台才是好平台?
- 机器学习笔记 - U-Net论文解读
- 2220. minimum number of bit flips to convert numbers
- 16. sum of the nearest three numbers - quick sort plus double pointer method
- Authentication successful processor
- When you are lonely, depressed and anxious Please remember, there is wind and rain
- Sequence model - 【 torch learning notes 】
- TensorFlow新文档发布:新增CLP、DTensor...最先进的模型已就绪!
- 跨域请求的问题
猜你喜欢

机器学习笔记 - R语言学习入门系列一

User directory one stop guide

【genius_platform软件平台开发】第三十七讲:网卡混杂模式和原始套接字

Machine learning notes - explore the keras dataset
![[genius_platform software platform development] lesson 35: UDP for cross network segment broadcasting](/img/53/c8d8b388788e13bdbf5cad9fbf91d6.png)
[genius_platform software platform development] lesson 35: UDP for cross network segment broadcasting

构建词表与抽样——【torch学习笔记】

Openstack explanation (XX) -- neutron node principle

TensorFlow新文档发布:新增CLP、DTensor...最先进的模型已就绪!

AI 考生挑战高考作文获 48 分;IBM 宣布退出俄罗斯市场,已暂停在俄所有业务;OpenCV 4.6 发布|极客头条...

Machine learning notes - create confusion matrix using scikit learn
随机推荐
MSF SSH protocol based information collection
Key configuration points of video fusion cloud service easycvr platform deployed in ECS
Openstack explanation (XIV) -- Grace keystone registration
16. sum of the nearest three numbers - quick sort plus double pointer method
2289. 使数组按非递减顺序排列
Iscc2022 challenge arena Misc
WPF 实现带明细的环形图表
【模型部署与业务落地】AI 框架部署方案之模型转换
科研入门,在导师完全散养不管的状态下,怎么提升自己的科研能力?
Troubleshooting of soaring memory utilization of redis cluster instances
The role of graphic edge detection in digital image processing in image understanding
Configuration based permission control
User directory one stop guide
1340. 跳跃游戏 V-动态规划加dfs
n-grams语言模型——【torch学习笔记】
从数据库查询权限信息
剑指 Offer 18. 删除链表的节点
Notes on the development of raspberry pie (15): Raspberry pie 4b+ compile and install MySQL database from the source code
Interview question 01.06 String compression
CSRF跨站请求伪造