应用场景,再跳转后不打开多个Tab的情况下,假如你需要从一个页面传一个固定参数跳转到另一个固定页面,进行数据筛选,就可以使用一下方式进行。
一、在要操作的页面进行如下声明:
1.先引入对应的命名空间和一些相关的注入。
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@inject ProtectedSessionStorage ProtectedSessionStore
2.假如当前页面只有一个按钮。用来进行点击跳转到其他页面。
<Button OnClick="@( async()=>{ await navToItem(SampleStatus.Default); })" style="width: 355px; z-index:333; height: 166px; background-color: #5470C6; position: relative" Color="Color.Light">
<p style="font-size: 12px; color: white;right:5px; top:15px; z-index: 999; position:absolute">点击查看更多</p>
</Button>
3.对ProtectedSessionStore进行相关配置
//声明一个key
private static string navKey = Guid.NewGuid().ToString("N");
//设置传入的参数,并绑定给key
private async Task navToItem(SampleStatus value)
{ //配置ProtectedSessionStore
await ProtectedSessionStore.SetAsync(navKey, value.ToString());
//跳转的页面
NavigationManager.NavigateTo($"_Sample/SlecteSample/{navKey}", WtmBlazor.Localizer["Text"]);
await Task.CompletedTask;
}
二、接收的页面,做如下操作
1.做相关声明
@page "/_Sample/SlecteSample/{navKey?}"
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
@inject ProtectedSessionStorage ProtectedSessionStore;
[Parameter]
public string? navKey { get; set; }
[Parameter]
public string? Status { get; set; }
2.通过ProtectedSessionStore.GetAsync进行接收
var res = await ProtectedSessionStore.GetAsync<string>(navKey);
Status = res.Value;
该方式,有点像Session传值,只不过一个是存在服务器,一个是客户端浏览器中。
本文介绍了如何在Blazor应用中,通过ProtectedSessionStorage组件在页面间传递参数,实现数据筛选功能,类似于服务器端的Session存储,但数据存储在客户端浏览器中。首先在源页面设置参数并跳转,然后在目标页面接收并使用这些参数。这种方法为跨页面通信提供了一种安全且便捷的方式。

被折叠的 条评论
为什么被折叠?



