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
相关产品推荐
相关产品推荐

