EdCharbeneau / BlazorSize

Blazor browser size interop for matchMedia and browser window size at runtime.
336 stars 39 forks source link

I always get an exception #103

Open IngoManthey opened 3 weeks ago

IngoManthey commented 3 weeks ago

I use Blazor WASM. I have installed

I register services.AddScoped<IResizeListener, ResizeListener>();

In the index.html:

My test page:

<h3>Test</h3>

@inject BlazorPro.BlazorSize.ResizeListener resizeListener

<h3>Browser Resize Example</h3>
<p>Current size: @currentSize</p>

@code {
    private string currentSize;

    protected override async Task OnInitializedAsync()
    {
        resizeListener.OnResized += WindowResized;
        var dimensions = await resizeListener.GetBrowserWindowSize();
        currentSize = $"Width: {dimensions.Width}, Height: {dimensions.Height}";
    }

    private void WindowResized(object sender, BrowserWindowSize e)
    {
        currentSize = $"Width: {e.Width}, Height: {e.Height}";
        InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        resizeListener.OnResized -= WindowResized;
    }
}

The exception: Uncaught (in promise) Error: System.ArgumentException: There is no event handler associated with this event. EventId: '545'. (Parameter 'eventHandlerId') at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetRequiredEventBindingEntry(UInt64 eventHandlerId) at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs, Boolean waitForQuiescence) at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs) at Microsoft.AspNetCore.Components.RenderTree.WebRenderer.WebRendererInteropMethods.DispatchEventAsync(JsonElement eventDescriptor, JsonElement eventArgs) at System.Object.InvokeStub_WebRendererInteropMethods.DispatchEventAsync(Object , Span`1 ) at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) --- End of stack trace from previous location --- at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(JSRuntime jsRuntime, DotNetInvocationInfo& callInfo, IDotNetObjectReference objectReference, String argsJson) at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.BeginInvokeDotNet(JSRuntime jsRuntime, DotNetInvocationInfo invocationInfo, String argsJson) at b.endInvokeDotNetFromJS (https://localhost:44392/_framework/blazor.webassembly.js:1:3136) at Object.gn [as endInvokeDotNetFromJS] (https://localhost:44392/_framework/blazor.webassembly.js:1:58943) at https://localhost:44392/_framework/dotnet.runtime.8.0.8.80cvijctdx.js:3:178428 at Ll (https://localhost:44392/_framework/dotnet.runtime.8.0.8.80cvijctdx.js:3:179262) at wasm://wasm/00b21c96:wasm-function[349]:0x1f99d at wasm://wasm/00b21c96:wasm-function[245]:0x1be41 at wasm://wasm/00b21c96:wasm-function[238]:0xf00e at wasm://wasm/00b21c96:wasm-function[272]:0x1d05f at wasm://wasm/00b21c96:wasm-function[3186]:0xe873e at wasm://wasm/00b21c96:wasm-function[2506]:0xbe4b2 Can anyone help me here, what am I doing wrong?

In the dev tool of chrome I see https://localhost:44392/_content/BlazorPro.BlazorSize/blazorSize.min.js error 404 what I can do??

ist not in the packed
EdCharbeneau commented 2 weeks ago

This line injects the concrete type: @inject BlazorPro.BlazorSize.ResizeListener resizeListener

It should be the interface IResizeListener: @inject BlazorPro.BlazorSize.IResizeListener resizeListener

There should be an exception on DI, however for some reason you're seeing a JS error. See if IResizeListener fixes your problem.