简体   繁体   English

Blazor - 同一组件的多个实例的 EventCallback 问题

[英]Blazor - Problem with EventCallback of multiple instances of same component

I'm having some problems with my C# Blazor App.我的 C# Blazor 应用程序有一些问题。

I've got a simple component that runs a js file, the js file after some operations will call the UpdateLocalizationData method.我有一个运行js文件的简单组件,js文件经过一些操作后会调用UpdateLocalizationData方法。 The method will populate the Place object and invoke the EventCallback.该方法将填充Place object 并调用 EventCallback。 This is the component AddressComponent :这是组件AddressComponent

@page "/AddressComponent"
@inject IJSRuntime JSRuntime


<input id="@Id" class="form-control" name="address" placeholder="Search address" />

@code {

    private static Place PlaceToUse { get; set; } = new Place();

    [Parameter] 
    public EventCallback<Place> OnPlaceSelected { get; set; }

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


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await JSRuntime.InvokeVoidAsync("initAutocomplete", DotNetObjectReference.Create(this), Id);
        }
    }

    [JSInvokable]
    public void UpdateLocalizationData(string lat, string lon)
    {    
        PlaceToUse.Id = Guid.NewGuid();
        PlaceToUse.Latitudine = lat;
        PlaceToUse.Longitudine = lon;

        OnPlaceSelected.InvokeAsync(PlaceToUse);
    }
}

Then i call the component in two different razor components.然后我在两个不同的 razor 组件中调用该组件。 PageA.razor :页A.razor

<AddressComponent OnPlaceSelected="HandlePlace" Id="addressA"></AddressComponent>

@code{
    private ItemA ItemATest { get; set; } = new ItemA()
    private void HandlePlace(Place place)
    {
        ItemATest.Place = place;
    }
}

PageB.razor :页B.razor

<AddressComponent OnPlaceSelected="HandlePlace" Id="addressB"></AddressComponent>

@code{
    private ItemB ItemBTest { get; set; } = new ItemB()
    private void HandlePlace(Place place)
    {
        ItemBTest.Place = place;
    }
}

Both components (PageA and PageB) are then called in the main page.然后在主页面中调用两个组件(PageA 和 PageB)。

My problem is that when the EventCallback method of the AddressComponent in PageA is triggered, it triggers the method HandlePlace of PageB .我的问题是,当PageAAddressComponentEventCallback方法被触发时,它会触发PageBHandlePlace方法。 From what i've understood, the parameters of the component (such as the EventCallback) are getting replaced everytime the component is instantiated, thus resulting in the Eventcallback of only the last component being triggered.据我了解,每次实例化组件时都会替换组件的参数(例如 EventCallback),从而导致仅触发最后一个组件的 Eventcallback。 How can i solve this?我该如何解决这个问题? Am i using the wrong approach?我使用了错误的方法吗?

The problem is inside the JS method that you call using InvokeVoidAsync.问题在于您使用 InvokeVoidAsync 调用的 JS 方法。 The first call sets the instance to PageA, the second call overwrites the instance to PageB.第一次调用将实例设置为 PageA,第二次调用将实例覆盖为 PageB。 So all calls made from the JS function will be done on PageB所以从 JS function 发出的所有调用都将在 PageB 上完成

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

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