C#Blazor错误BL0005-正确设置组件属性 [英] C# Blazor Error BL0005 - Setting Component Properties Properly

查看:113
本文介绍了C#Blazor错误BL0005-正确设置组件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对这个错误进行了研究,只是不了解正确的解决方案.

I have done the research I can on this error and just not understanding the correct solution.

示例代码(直接从供应商示例中获取)
https://blazor.syncfusion.com/documentation/menu-bar/getting-started/

Example code (taken directly from the vendors sample)
https://blazor.syncfusion.com/documentation/menu-bar/getting-started/

<SfMenu Items="@MenuItems"></SfMenu>

@code {
    public List<MenuItem> MenuItems = new List<MenuItem>{
        new MenuItem{ Text = "File", Items = new List<MenuItem>{
            new MenuItem{ Text= "Open" },
            new MenuItem{ Text= "Save" },
            new MenuItem{ Text= "Exit" }}
    },
        new MenuItem{ Text = "Edit", Items = new List<MenuItem>{
            new MenuItem{ Text= "Cut" },
            new MenuItem{ Text= "Copy" },
            new MenuItem{ Text= "Paste" }}
    },
        new MenuItem{ Text = "View", Items = new List<MenuItem>{
            new MenuItem{ Text = "Toolbars" },
            new MenuItem{ Text = "Zoom" },
            new MenuItem{ Text = "Full Screen" }}
    },
        new MenuItem{ Text = "Tools", Items = new List<MenuItem>{
            new MenuItem{ Text= "Spelling & Grammar" },
            new MenuItem{ Text= "Customize" },
            new MenuItem{ Text= "Options" }}
    },
        new MenuItem{ Text = "Go" },
        new MenuItem{ Text = "Help" }
    };
}

这将生成BL0005组件参数文本",不应在其组件外部设置.它将使用所有参数来做到这一点.

This will generate the BL0005 Component parameter 'Text' should not be set outside it's component. It will do that with all of the parameters.

这是将变量设置为传递给子组件的代码.我看不到其他设置方式.该组件没有提供单独的设置器.我真的不明白VS团队希望我如何构建和传递这些数据(很明显,如果供应商的代码编译时带有相同的警告,那么供应商也不会这样做).

This is code setting the variable to pass to the child component. I see no other way of setting this. There is no individual setter provided by the component. I really don't understand how the VS team wants me to build and pass this data (and obviously the vendor doesn't either if their code compiles with the same warnings).

谁能提供一个具体的示例说明该怎么做?
我已阅读在blazor中突变组件属性的正确方法,但由于它是第三方库,因此在这种情况下我显然不能修改子组件.

Can anyone provide a concrete example of how this should be done?
I have read Correct way to mutate a component property in blazor but I obviously can't modify the child component in this case since it is a third party library.

推荐答案

用[Parameter]装饰的属性不应通过代码设置,因为不会调用OnParametersSet生命周期方法,因此仅应将它们设置为由父组件在剃刀标记中设置.

Properties decorated with [Parameter] aren't supposed to be set via code because the OnParametersSet lifecycle methods won't get called, so they are only supposed to be set by the parent component in razor markup.

在您的情况下,因为值不会更改,所以可以忽略这些警告.如果您在文件后面的代码(MyComponent.razor.cs)中创建菜单项,则可以执行此操作

In your case, because the values won't change it should be okay to ignore those warnings. If you create the menu items in a code behind file (MyComponent.razor.cs) you can do this

public partial class MyComponent
{
  protected override void OnInitialized() 
  {
#pragma warning disable BL0005
    // create the menu structure here
#pragma warning restore BL0005
  } 
} 

这篇关于C#Blazor错误BL0005-正确设置组件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆