Blazor中如何在组件中使用[Parameter]属性?

huangapple go评论85阅读模式
英文:

How do I use Blazor [Parameter] attribute in a component?

问题

抱歉,我将只翻译代码部分,不提供其他内容:

  1. 使用 `[Parameter]` 特性在 Razor 类库中的 Razor 组件中的变量上使用时出现错误。
  2. 我得到的错误是:
  3. ```plaintext
  4. Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  5. 未处理的异常渲染组件: 类型为 'DashboardComponents.LineGraph' 的对象没有与名称 'Values' 匹配的属性。
  6. System.InvalidOperationException: 类型为 'DashboardComponents.LineGraph' 的对象没有与名称 'Values' 匹配的属性。
  7. Microsoft.AspNetCore.Components.Reflection.ComponentProperties.ThrowForUnknownIncomingParameterName(Type targetType, String parameterName)
  8. Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
  9. Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(Object target)
  10. Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
  11. Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters)

但我不确定为什么它说没有与 Values 匹配的属性,因为它在 LineGraph.razor.cs 文件中明确定义如下:

  1. namespace DashboardComponents;
  2. public partial class LineGraph
  3. {
  4. [Parameter, EditorRequired]
  5. public static List<double> Values { get; set; } = new();
  6. [Parameter, EditorRequired]
  7. public static double Scale { get; set; }
  8. [Parameter]
  9. public static double GraphWidth { get; set; } = 500;
  10. [Parameter]
  11. public static double GraphHeight { get; set; } = 300;
  12. [Parameter] public ComponentColors Colors { get; set; } = new();
  13. }

我在另一个 Blazor WASM 项目中进行测试时,它的使用方式如下:

  1. @page "/"
  2. @using DashboardComponents
  3. <LineGraph Values="@Values" Scale="5" />
  4. @code {
  5. private readonly List<double> Values = new()
  6. {
  7. 0,
  8. 7,
  9. 5.5,
  10. 3.4,
  11. 9
  12. };
  13. }

我尝试过更改变量名称,测试 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:

  1. Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  2. Unhandled exception rendering component: Object of type 'DashboardComponents.LineGraph' does not have a property matching the name 'Values'.
  3. System.InvalidOperationException: Object of type 'DashboardComponents.LineGraph' does not have a property matching the name 'Values'.
  4. at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.ThrowForUnknownIncomingParameterName(Type targetType, String parameterName)
  5. at Microsoft.AspNetCore.Components.Reflection.ComponentProperties.SetProperties(ParameterView& parameters, Object target)
  6. at Microsoft.AspNetCore.Components.ParameterView.SetParameterProperties(Object target)
  7. at Microsoft.AspNetCore.Components.ComponentBase.SetParametersAsync(ParameterView parameters)
  8. 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:

  1. namespace DashboardComponents;
  2. public partial class LineGraph
  3. {
  4. [Parameter, EditorRequired]
  5. public static List<double> Values { get; set; } = new();
  6. [Parameter, EditorRequired]
  7. public static double Scale { get; set; }
  8. [Parameter]
  9. public static double GraphWidth { get; set; } = 500;
  10. [Parameter]
  11. public static double GraphHeight { get; set; } = 300;
  12. [Parameter] public ComponentColors Colors { get; set; } = new();
  13. }

And where I'm testing it in another Blazor WASM project, it is used like this:

  1. @page "/"
  2. @using DashboardComponents
  3. <LineGraph Values="@Values" Scale="5" />
  4. @code {
  5. private readonly List<double> Values = new()
  6. {
  7. 0,
  8. 7,
  9. 5.5,
  10. 3.4,
  11. 9
  12. };
  13. }

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]属性。所有属性应该是非静态的。

  1. // this
  2. [Parameter, EditorRequired]
  3. public static List<double> Values { get; set; } = new();
  4. // would become
  5. [Parameter, EditorRequired]
  6. public List<double> Values { get; set; } = new();
英文:

You can't use the [Parameter] attribute on a static property. All properties should be non-static.

  1. // this
  2. [Parameter, EditorRequired]
  3. public static List<double> Values { get; set; } = new();
  4. // would become
  5. [Parameter, EditorRequired]
  6. public List<double> Values { get; set; } = new();

huangapple
  • 本文由 发表于 2023年3月7日 18:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定