You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor中如何配置带IsFixed的根级联值?含命名级联疑问

Blazor根级联值isFixed模式下指定名称的正确方式

问题原因

当使用CascadingValueSource配置固定(isFixed: true)的根级联值时,AddCascadingValue(string name, ...)重载中的名称参数不会生效——框架会优先读取CascadingValueSource实例自身的Name属性,而非方法传入的名称参数,这就是之前写法导致组件接收值为null的核心原因。

正确写法

直接在CascadingValueSource的构造函数中指定名称,或者通过属性设置名称:

方式一:构造函数指定名称

builder.Services.AddCascadingValue(s =>
{
    var currentSeason = new CurrentSeasonModel { SelectedStageId = 65 };
    // 构造函数中传入名称、值和isFixed参数
    var source = new CascadingValueSource<CurrentSeasonModel>(
        name: "abc",
        value: currentSeason,
        isFixed: true);
    return source;
});

方式二:属性设置名称

builder.Services.AddCascadingValue(s =>
{
    var currentSeason = new CurrentSeasonModel { SelectedStageId = 65 };
    var source = new CascadingValueSource<CurrentSeasonModel>(currentSeason, isFixed: true)
    {
        Name = "abc"
    };
    return source;
});

组件接收代码(无需修改)

[CascadingParameter(Name = "abc")]
public CurrentSeasonModel? hehe { get; set; }

内容的提问来源于stack exchange,提问作者nam vo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 23:50:08