英文:
How do I use Blazor [Parameter] attribute in a component?
问题
抱歉,我将只翻译代码部分,不提供其他内容:
使用 `[Parameter]` 特性在 Razor 类库中的 Razor 组件中的变量上使用时出现错误。
我得到的错误是:
```plaintext
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
未处理的异常渲染组件: 类型为 'DashboardComponents.LineGraph' 的对象没有与名称 'Values' 匹配的属性。
System.InvalidOperationException: 类型为 'DashboardComponents.LineGraph' 的对象没有与名称 'Values' 匹配的属性。
在 Microsoft.AspNetCore.Components.Reflection.ComponentProperties.ThrowForUnknownIncomingParameterName(Type targetType, String parameterName)
在 Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
在 Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(Object target)
在 Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
在 Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters)
但我不确定为什么它说没有与 Values
匹配的属性,因为它在 LineGraph.razor.cs 文件中明确定义如下:
namespace DashboardComponents;
public partial class LineGraph
{
[Parameter, EditorRequired]
public static List<double> Values { get; set; } = new();
[Parameter, EditorRequired]
public static double Scale { get; set; }
[Parameter]
public static double GraphWidth { get; set; } = 500;
[Parameter]
public static double GraphHeight { get; set; } = 300;
[Parameter] public ComponentColors Colors { get; set; } = new();
}
我在另一个 Blazor WASM 项目中进行测试时,它的使用方式如下:
@page "/"
@using DashboardComponents
<LineGraph Values="@Values" Scale="5" />
@code {
private readonly List<double> Values = new()
{
0,
7,
5.5,
3.4,
9
};
}
我尝试过更改变量名称,测试 Razor 文件中使用 @Value
,使用不同的 [Parameter]
类型,但似乎没有什么效果。
英文:
Using the [Parameter]
attribute on a variable in a Razor component in a Razor Class Library errors out when I use it.
The error I get is:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Object of type 'DashboardComponents.LineGraph' does not have a property matching the name 'Values'.
System.InvalidOperationException: Object of type 'DashboardComponents.LineGraph' does not have a property matching the name 'Values'.
at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.ThrowForUnknownIncomingParameterName(Type targetType, String parameterName)
at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
at Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(Object target)
at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters)
But I'm not sure why it says there is no property matching Values
since it's defined right in the LineGraph.razor.cs file:
namespace DashboardComponents;
public partial class LineGraph
{
[Parameter, EditorRequired]
public static List<double> Values { get; set; } = new();
[Parameter, EditorRequired]
public static double Scale { get; set; }
[Parameter]
public static double GraphWidth { get; set; } = 500;
[Parameter]
public static double GraphHeight { get; set; } = 300;
[Parameter] public ComponentColors Colors { get; set; } = new();
}
And where I'm testing it in another Blazor WASM project, it is used like this:
@page "/"
@using DashboardComponents
<LineGraph Values="@Values" Scale="5" />
@code {
private readonly List<double> Values = new()
{
0,
7,
5.5,
3.4,
9
};
}
I've tried changing the variable name around, using @Value
in the testing razor file, using the different [Parameter]
types, but nothing wants to work.
答案1
得分: 1
你不能在静态属性上使用[Parameter]
属性。所有属性应该是非静态的。
// this
[Parameter, EditorRequired]
public static List<double> Values { get; set; } = new();
// would become
[Parameter, EditorRequired]
public List<double> Values { get; set; } = new();
英文:
You can't use the [Parameter]
attribute on a static property. All properties should be non-static.
// this
[Parameter, EditorRequired]
public static List<double> Values { get; set; } = new();
// would become
[Parameter, EditorRequired]
public List<double> Values { get; set; } = new();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论