如何正确使用Blazor的bind:set和bind:get?

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

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' 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 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&lt;string&gt;' to 'System.Action&lt;string?&gt;'

My code:

&lt;input type=&quot;text&quot;
    @bind:get=&quot;text&quot;
    @bind:set=&quot;SetValue&quot; /&gt;
    
@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:

&lt;input type=&quot;text&quot;
    @bind=&quot;Text&quot; /&gt;

@code {
    private string Text
    {
        get =&gt; text;
        set =&gt; 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:

如何正确使用Blazor的bind:set和bind:get?

Running:

如何正确使用Blazor的bind:set和bind:get?

如何正确使用Blazor的bind:set和bind:get?

答案2

得分: 0

这是一个已知的问题。根据这个评论中的建议,我通过安装.NET 8 Preview 2成功使bind:get/bind:set工作。

英文:

This is a know issue. I was able to make bind:get/bind:set work by installing .NET 8 Preview 2 as suggested in this comment.

huangapple
  • 本文由 发表于 2023年4月6日 22:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950489.html
匿名

发表评论

匿名网友

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

确定