1

I don't want to implement my logic in OnInitializedAsync, which happens on initial load. This should happen after the initial load and when user start resizing the browser window.

Thanks.

1

1 Answer 1

5

The answer is that you catch the event in Javascript, and then call a Blazor method through JS Interop.

However, I strongly recommend you NOT handle events that could fire very frequently, like mouse position changes or window size changes, with constant calls back to Blazor. Handle those using pure Javascript, and call a Blazor method after some sensible completion-- the mouse button has been released, or a certain delay time has passed, etc.

This is especially true in server flavor, and with insidious consequences. You could have a site that works fine in development, but fails horribly when deployed, do to the higher latency of a distant server.

3
  • Up voted because this is really the only solution. .NET 6.0 adds a way to create custom event handlers (see:learn.microsoft.com/en-us/aspnet/core/blazor/components/… ), but it can't be used with the resize event since that only occurs on the window object (not on elements).
    – Yogi
    Mar 24, 2022 at 13:17
  • 1
    Thanks. Ended up following this tutorial. The only way to do it atm is using Javascript.
    – dev00001
    Apr 5, 2022 at 13:42
  • The tutorial linked by @dev00001 works, though you'll have to also look at the comment on it. The service needs to be declared as scoped, not singleton. And you'll need to use a class derived from EventArgs if you want to get the width and height, then pass that into the Invoke call rather than just the width.
    – Alex
    Nov 15, 2022 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.