英文:
How to use Blazor bind:set and bind:get correctly?
问题
I am trying to use the new blazor value-binding with separate get and set.
Somehow my code does not work and I get this compiler-exception:
Argument 3: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback
My code:
<input type="text"
@bind:get="text"
@bind:set="SetValue" />
@code {
private string text = string.Empty;
private void SetValue(string value)
{
text = value;
}
}
I tried to use this.text in the @bind:get and I tried to use @bind-value but it did not change anything
Meanwhile, this code works totally fine:
<input type="text"
@bind="Text" />
@code {
private string Text
{
get => text;
set => SetValue(value);
}
private string text;
private void SetValue(string text)
{
this.text = text;
}
}
Update: The second code snippet does not work anymore (it does not show the value correctly in the input field)
英文:
I am trying to use the new blazor value-binding with separete get and set.
Somehow my code does not work and I get this compiler-exception:
Argument 3: cannot convert from 'Microsoft.AspNetCore.Components.EventCallback<string>' to 'System.Action<string?>'
My code:
<input type="text"
@bind:get="text"
@bind:set="SetValue" />
@code {
private string text = string.Empty;
private void SetValue(string value)
{
text = value;
}
}
I tried to use this.text in the @bind:get and i tried to use @bind-value but it did not change anything
Meanwhile, this code works totally fine:
<input type="text"
@bind="Text" />
@code {
private string Text
{
get => text;
set => SetValue(value);
}
private string text;
private void SetValue(string text)
{
this.text = text;
}
}
Update: The second code snipped does not work anymore (it does not show the value correct in the input field)
答案1
得分: 1
"Pre March 2023 releases had the issue you have highlighted, but it was fixed in the latest 17.5 release. See the screen shots below.
No Errors showing:"
"2023年3月之前的版本存在你所指出的问题,但在最新的17.5版本中已经修复。请查看下面的屏幕截图。"
英文:
You need to make sure you have the latest updates to Visual Studio and Net7.0. @bind:after
is another story!
Pre March 2023 releases had the issue you have highlighted, but it was fixed in the latest 17.5 release. See the screen shots below.
No Errors showing:
Running:
答案2
得分: 0
这是一个已知的问题。根据这个评论中的建议,我通过安装.NET 8 Preview 2成功使bind:get
/bind:set
工作。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论