Blazor-Navigating 组件

介绍

Blazor 中的 Navigating 组件是路由系统的一部分,主要用于在页面导航发生但尚未完成时向用户提供视觉反馈(如加载提示)。它封装在 Router 组件内部,是增强用户体验的关键工具。
Navigating组件,其功能是当 Blazor 应用程序在切换页面时的中间等待界面,可能由于网络缓慢,或其他原因导致。

核心用途

显示导航状态

当用户触发页面跳转(点击链接、代码调用 NavigationManager.NavigateTo)时,若目标页面需要异步加载(如下载程序集、初始化数据),Navigating 组件会立即显示预设内容(如加载动画),避免界面“卡顿”的错觉。

提升用户体验

在导航延迟期间(如网络请求慢),通过提示告知用户操作正在处理,减少重复点击或误操作。

使用场景

1. 异步加载页面

场景:首次访问某个页面时,需从服务器下载 .dll 程序集。
解决方案:Navigating 显示加载动画,直到程序集下载完成。

2. 数据初始化耗时

场景:目标页面的 OnInitializedAsync() 方法需调用 API 获取数据。
解决方案:导航期间显示占位内容,避免空白页面。

实例实现

在App.razor中设置跳转等待过程中的界面,我们做一个延时的界面看看实际的效果
在 Blazor Web App 任何呈现模式的每页/组件交互位置的项目中,Navigating 组件是无效的。

<Router AppAssembly="@typeof(App).Assembly">
  <Found Context="routeData">
    <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
        <NotFound>
          <PageTitle>Not found</PageTitle>
          <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">Sorry, there's nothing at this address.</p>
          </LayoutView>
        </NotFound>
        <Navigating>
          <h1>等待跳转中......</h1>
        </Navigating>
      </Router>
<Navigating>
    <h1>等待跳转中......</h1>
</Navigating>

我们添加了一个跳转的延时操作

<Router AppAssembly="@typeof(App).Assembly" OnNavigateAsync="Callback">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
    <Navigating>
        <h1>等待跳转中......</h1>
    </Navigating>
</Router>
@code {
    private async Task Callback(NavigationContext obj)
    {
        await Task.Delay(3000);
    }
}

运行结果

在这里插入图片描述

往期推荐

以下的文章和本文章也有着关联性,欢迎大家阅读
Blazor-全局路由跳转事件

### MAUI WebView Navigating Event Usage and Examples The `Navigating` event of the `WebView` control in .NET Multi-platform App UI (.NET MAUI) allows developers to handle actions when a navigation is about to occur within the web view. This can be useful for intercepting or canceling navigations based on specific conditions. To use this feature, one must first ensure that an instance of `WebView` exists either through XAML definition or programmatically as shown below: ```csharp // Programmatically creating a WebView with specified dimensions. var webView = new WebView { Source = "https://www.example.com", }; webView.HeightRequest = DeviceDisplay.Current.MainDisplayInfo.Height / 3; webView.WidthRequest = DeviceDisplay.Current.MainDisplayInfo.Width; layout.Children.Add(webView); ``` After setting up the `WebView`, attaching an event handler to the `Navigating` event enables interception of all navigation attempts before they start[^4]. Here's how it looks in practice: ```csharp webView.Navigating += (sender, e) => { Console.WriteLine($"Attempting to navigate from {e.Source} to {e.Url}"); // Optionally prevent certain types of navigation by checking properties like Url. if (!string.IsNullOrEmpty(e.Url)) { var uriResult = Uri.TryCreate(e.Url, UriKind.Absolute, out _); if (!uriResult || !Uri.IsWellFormedUriString(e.Url, UriKind.Absolute)) e.Cancel = true; // Cancel invalid URL navigations. // Additional logic could go here such as showing confirmation dialogs etc. } }; ``` This approach provides fine-grained control over what happens during each attempted page load inside the embedded browser component while ensuring only valid URLs are allowed to proceed[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code-Study

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值