简体   繁体   English

Blazor 中的“无法从绑定属性‘绑定值’推断属性名称”错误

[英]"The attribute names could not be inferred from bind attribute 'bind-value'" error in Blazor

I've just migrated a Blazor project from Core 3 Preview 6 to Preview 8 and I'm now getting this error:我刚刚将 Blazor 项目从 Core 3 Preview 6 迁移到 Preview 8,现在出现此错误:

The attribute names could not be inferred from bind attribute 'bind-value'.无法从绑定属性“绑定值”推断属性名称。 Bind attributes should be of the form 'bind' or 'bind-value' along with their corresponding optional parameters like 'bind-value:event', 'bind:format' etc.绑定属性应采用“bind”或“bind-value”形式以及它们相应的可选参数,如“bind-value:event”、“bind:format”等。

I've isolated the component that's causing this to happen, and the code certainly seems to bind-value set as per the instructions in the error message:我已经隔离了导致这种情况发生的组件,并且代码当然似乎按照错误消息中的说明设置了bind-value

      <TelerikDropdownList Data="@State.ContainerSizes" 
                           ValueField=@nameof(ContainerSize.ContainerSizeId)
                           TextField=@nameof(ContainerSize.ContainerSizeName)
                           @bind-Value="@ContainerSizeIdNoNull"
                           >
      </TelerikDropdownList>

I've tried removing the @ from @bind-Value and changing the capitalisation @bind-Value etc. but all to no avail.我尝试从@bind-Value中删除@并更改大写@bind-Value等,但都无济于事。

What can be causing this?这可能是什么原因造成的?

It turns out there are at least two causes of this:事实证明,至少有两个原因:

The component name is now case-sensitive组件名称现在区分大小写

The answer turns out to be that naming of blazor components is now case-sensitive, and I was missing a capital letter in 'TelerikDropdownList' which should be TelerikDrop D ownList.答案是 blazor 组件的命名现在区分大小写,我在“TelerikDropdownList”中遗漏了一个大写字母,它应该是 TelerikDrop D ownList。

The change to use case-sensitive names is documented here and is also discussed here and in the official documentation here .在使用名称区分大小写的变化此处介绍并讨论在这里和正式文件在这里 The idea was to reduce misleading messages, but it's had the consequence of introducing another one, so I've raised an issue for that on the AspNetCore repo .这个想法是为了减少误导性消息,但它的结果是引入了另一个,所以我在 AspNetCore repo 上提出了一个问题

You've forgotten the @using statement for the component's namespace你忘记了组件命名空间的@using语句

You'll also get the same error if you've forgotten or removed the @using statement for the component's namespace.如果您忘记或删除了组件命名空间的@using语句,您也会得到同样的错误。 That's very easy to do if you're using ReSharper as it is currently flagging them as unused and offering to remove them.如果您正在使用 ReSharper,这很容易做到,因为它目前将它们标记为未使用并提供删除它们。

Checking if this is the issue检查这是否是问题

A good way to check if the compiler has correctly identified your component as a Blazor component rather than a HTML tag is to check the colour coding of the keywords.检查编译器是否正确地将您的组件识别为 Blazor 组件而不是 HTML 标记的好方法是检查关键字的颜色编码。 They will be the same colour if things are working correctly (green in the example below):如果一切正常,它们将是相同的颜色(在下面的示例中为绿色):

在此处输入图片说明

Whereas if the name or namespace are wrong you'll see a mix of colours (note that Data and ValueField are now a different colour to TelerikDropdownList ):而如果名称或名称空间是错误的,你会看到的颜色(注意,混合DataValueField现在不同的颜色TelerikDropdownList ):

在此处输入图片说明

“The attribute names could not be inferred from bind attribute 'bind-value'” exception in Blazor “无法从 Blazor 中的绑定属性‘绑定值’推断出属性名称”

I had a similar issue, but the solution was rather easy than intuitive!我有一个类似的问题,但解决方案相当简单而不是直观!

Finally I found the information that adding a missing using statement of the used component was helpful.最后我发现添加一个缺少的使用组件的 using 语句的信息是有帮助的。 so did I. And it worked!我也是。它奏效了!

Despite the component name was shown as green (like it is recognized) it wasn't.尽管组件名称显示为绿色(就像被识别一样),但事实并非如此。 Only the missing using solved the problem.只有缺少 using 解决了问题。 Such a missleading behavior.这种误导行为。

So if you have the same problem, check if you're missing a 'using' for the actual component even if the component's name is shown as green.因此,如果您遇到同样的问题,请检查您是否遗漏了实际组件的“使用”,即使组件的名称显示为绿色。

In my case I had following parameters:就我而言,我有以下参数:

[Parameter]
public string[] BindingValue { get; set; }

[Parameter]
public EventCallback<string[]> BindingValueChanged { get; set; }

And the binding:和绑定:

<MultiselectDropdownComponent 
@bind-BindingValue="SessionState.MyArray" />

Was producing the same error as in subject.产生与主题相同的错误。 I had a using statement specified as well..我也指定了一个using语句..

From the MS documentation: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-5.0#binding-with-component-parameters来自 MS 文档: https : //docs.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-5.0#binding-with-component-parameters

<Child @bind-Year="year" />

The Year parameter is bindable because it has a companion YearChanged event that matches the type of the Year parameter. Year 参数是可绑定的,因为它有一个与 Year 参数类型匹配的伴随 YearChanged 事件。

By convention, a property can be bound to a corresponding event handler by including an @bind-{PROPERTY}:event attribute assigned to the handler.按照惯例,属性可以通过包含分配给处理程序的 @bind-{PROPERTY}:event 属性绑定到相应的事件处理程序。 <Child @bind-Year="year" /> is equivalent to writing: <Child @bind-Year="year" /> 相当于写:

<Child @bind-Year="year" @bind-Year:event="YearChanged" />

So I decided to explicitly specify the event and it worked!所以我决定明确指定event并且它起作用了!

<MultiselectDropdownComponent 
@bind-BindingValue="SessionState.MyArray"
@bind-BindingValue:event="BindingValueChanged" />

edit: using Blazor WASM and .Net 5编辑:使用Blazor WASM 和 .Net 5

I can add another - not obvious pitfall (at least to me).我可以添加另一个 - 不明显的陷阱(至少对我而言)。

Fully qualifying the component, and next relying on the using statement to identify the properties does not work.完全限定组件,然后依靠 using 语句来识别属性不起作用。 This got added by intellisense.这是由智能感知添加的。

Not working example:不工作的例子:

@using WebUI.Components.Modals
<WebUI.Components.Modals.WebUI.Components.Modals.AssetModal @bind-IsVisible="_assetDialogVisible" Asset="_selectedAsset"></WebUI.Components.Modals.WebUI.Components.Modals.AssetModal>

Working version:工作版本:

@using WebUI.Components.Modals
<AssetModal @bind-IsVisible="_assetDialogVisible" Asset="_selectedAsset"></AssetModal>

I changed @bind-Value to @bind-value (lowercase) and it worked.我将@bind-Value更改为@bind-value (小写)并且它起作用了。

i imported the library @using Syncfusion.Blazor.DropDowns我导入了库@using Syncfusion.Blazor.DropDowns

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM