EditContext.OnFieldChanged 为什么不触发?

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

Why is EditContext.OnFieldChanged not firing?

问题

我已经按如下方式设置了我的代码:

protected override async Task OnInitializedAsync()
{
    Value ??= new AddressFormPageModel();
    Context = new EditContext(Value);
    Context.OnFieldChanged += OnFieldChanged;
}

private async void OnFieldChanged(object? sender, FieldChangedEventArgs e)
{
    IsValidated = false;
    if (ValueChanged.HasDelegate)
        await ValueChanged.InvokeAsync(Value);
}

而在我的Razor文件中,我有以下内容:

<DxFormLayout>
    <DxFormLayoutItem Caption="Street Address:" Field="@nameof(Value.StreetNumberAndName)" ColSpanMd="12">
        <DxTextBox @bind-Text="@Value!.StreetNumberAndName" 
                   ReadOnly="@ReadOnly"
                   ShowValidationIcon="true" />
    </DxFormLayoutItem>
    ...

当我编辑“Street Address:”并退出时,OnFieldChanged 没有被调用。当我调用 Submit 时,Value对象被正确地填充了 StreetNumberAndName 的值。

我在Value.StreetNumberAndName的setter上设置了断点,当我从编辑框切换到下一个时,它被设置。

有没有任何想法为什么 OnFieldChanged 没有被调用?

更新:

这是一个内部组件。它在以下位置:

<EditForm EditContext="@EditContext"
          OnValidSubmit="@HandleValidSubmitAsync"
          OnInvalidSubmit="@HandleInvalidSubmitAsync"
          Context="@EditFormContext">
    <ObjectGraphDataAnnotationsValidator />
    <CustomValidation @ref="_customValidation" />
    <DxFormLayout>
        <DxFormLayoutItem Caption="Unique Id:" Field="@nameof(Model.UniqueId)" ColSpanMd="4">
            <DxTextBox @bind-Text="@Model.UniqueId"
                       ReadOnly="@(ModeState != Mode.Create)"
                       ShowValidationIcon="true" />
        </DxFormLayoutItem>

        <AddressForm @ref="_addressElement"
                     ReadOnly="@(ModeState == Mode.Read)"
                     OnValidated="@OnAddressValidated"
                     ValueChanged="@OnAddressChanged"
                     Value="@Model.AddressModel">
        </AddressForm>
    </DxFormLayout>
    ...

希望这可以帮助您解决 OnFieldChanged 没有被调用的问题。

英文:

I have set up my code as follows:

protected override async Task OnInitializedAsync()
{
	Value ??= new AddressFormPageModel();
	Context = new EditContext(Value);
	Context.OnFieldChanged += OnFieldChanged;
}

private async void OnFieldChanged(object? sender, FieldChangedEventArgs e)
{
	IsValidated = false;
	if (ValueChanged.HasDelegate)
		await ValueChanged.InvokeAsync(Value);
}

And in my razor file I have:

&lt;DxFormLayout &gt;
	&lt;DxFormLayoutItem Caption=&quot;Street Address:&quot; Field=&quot;@nameof(Value.StreetNumberAndName)&quot; ColSpanMd=&quot;12&quot;&gt;
		&lt;DxTextBox @bind-Text=&quot;@Value!.StreetNumberAndName&quot; 
				   ReadOnly = &quot;@ReadOnly&quot;
		           ShowValidationIcon=&quot;true&quot; /&gt;
	&lt;/DxFormLayoutItem&gt;
...

When I edit the "Street Address:" and exit it, the OnFieldChanged is not called. When I call Submit the Value object is correctly populated with the StreetNumberAndName value.

I did set a breakpoint on the Value.StreetNumberAndName setter and it is set when I tab from that edit box to the next.

Any idea why OnFieldChanged is not being called?

Update:

This is an inner component. It is &lt;AddressForm&gt; in the following:

&lt;EditForm EditContext=&quot;EditContext&quot;
		  OnValidSubmit=&quot;HandleValidSubmitAsync&quot;
		  OnInvalidSubmit=&quot;HandleInvalidSubmitAsync&quot;
		  Context=&quot;EditFormContext&quot;&gt;
	&lt;ObjectGraphDataAnnotationsValidator /&gt;
	&lt;CustomValidation @ref=&quot;_customValidation&quot; /&gt;
	&lt;DxFormLayout&gt;
		&lt;DxFormLayoutItem Caption=&quot;Unique Id:&quot; Field=&quot;@nameof(Model.UniqueId)&quot; ColSpanMd=&quot;4&quot;&gt;
			&lt;DxTextBox @bind-Text=&quot;@Model.UniqueId&quot;
					   ReadOnly=&quot;@(ModeState != Mode.Create)&quot;
					   ShowValidationIcon=&quot;true&quot; /&gt;
		&lt;/DxFormLayoutItem&gt;

		&lt;AddressForm @ref=&quot;_addressElement&quot;
		             ReadOnly=&quot;@(ModeState == Mode.Read)&quot;
		             OnValidated=&quot;OnAddressValidated&quot;
					 ValueChanged=&quot;@OnAddressChanged&quot;
					 Value=&quot;@Model.AddressModel&quot;&gt;
		&lt;/AddressForm&gt;
...

答案1

得分: 0

父 EditContext 需要传递给子组件。它不能自己创建。如何做到这一点在这里回答。

英文:

World's shortest answer - the parent EditContext needs to be passed down to the child component. It cannot make its own.

How to do this answered here.

huangapple
  • 本文由 发表于 2023年6月13日 00:04:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458427.html
匿名

发表评论

匿名网友

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

确定