我声明了一个带有可选参数的 Razor 页面:
[Parameter]
public KeyValuePair<string, T>? Value { get; set; }
我在 Razor 页面标记中使用的:
@if (Value != null && item.Key == Value.Key)
{
<span>
.....
</span>
}
Visual Studio 抱怨用“?”声明的可空类型(如果我删除“?”,就没有错误):
CS1061 'KeyValuePair<string, T>?' does not contain a definition for 'Key' and no accessible extension method 'Key' accepting a first argument of type 'KeyValuePair<string, T>?'
它似乎忽略了我的“Value!= null”测试。修复此错误的“Blazor”方法是什么?
KeyValuePair<K,V>
是一个值类型(结构)。
当您使用可为空值类型时,您将必须处理 Value 属性:
public KeyValuePair<string, T>? Value { get; set; }
@if (Value != null && item.Key == Value.Value.Key)