Skip to content
Advertisement

Cannot Move Divs in Blazor

I have a Blazor Server App with the following DIV tags

JavaScript

I attempted to set the Height, Width, and X/Y coordinates using the code samples from this page – https://blazor.tips/blazor-how-to-ready-window-dimensions/ but that never worked and simply threw an uncaught exception no matter where I placed Try… blocks.

I then moved to a more straightforward JS call:

JavaScript

If I make this call directly in the code, nothing ever happens – even with the OnInitializeAsync commented out.

JavaScript

If I place this method in the OnAfterRendor method:

JavaScript

everything hangs until I kill the project. There are never any errors but the code never runs when I place breakpoints on the height or width statements.

I even added in the Async version to no avail:

JavaScript

Same result as everything hangs. I am at a complete loss as to how to proceed and I really could use some help!

As a point of clarity, it is the call to the JS that results in the hang:

JavaScript

I added in some alerts but they never run:

JavaScript

Thank you for your time!

BTW – I did change the double await to string data = JSRuntime.InvokeAsync<string>("getMyWindow", new object[] { }).Result;

UPDATE: I moved the JS call outside of the await altogether and I got the error:

InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.

In this case, I am literally calling the method from the OnAfterRenderAsync method:

JavaScript

Advertisement

Answer

Not sure what you want… Copy the code below and run it, and tell us if that is what you were trying to get.

Index.razor

JavaScript

Note: The OnInitialized{Async} pair of methods are the life-cycle methods of the base class ComponentBase. They are automatically called by the Blazor framework when a Razor component is being created. They are executed only once. You may override them, and add your logics, but you SHOULD never call them manually from your code.

This:

JavaScript

This is wrong and must never be done. You should not call OnAfterRender{Async}. It is the Blazor framework that should call OnAfterRender{Async}, not the developer. Could you try to comprehend what your code is doing…

Try to understand that though the Razor components are defined as C# classes, they are special cases of Classes, that require special handling by the framework…

Update

Ken Tola, the following code I believe does what you’re looking for. It reads the width and height of the window object, pass it to the Index component, and relocate your dear divs. Note that before the app relocates the divs, I check the values of the width and height, and determine the dimensions of the divs. This is of course is done for demonstration purposes, and you can manipulate those values as you wish…

Index.razor

JavaScript

BrowserService.cs

JavaScript

Startup.ConfigureServices

services.AddScoped<BrowserService>();

_Host.cshtml

JavaScript

Note: Consider handling the relocation of the div elements when the window is resized. It should be responsive, right ? Not sure that in your case you can employ media query. Any how, as you can see, I have designed the code in such a way that it takes into account that your div elements may need to be relocate again and again, thus it constantly (when resizing) notifies your Index component of the changing dimensions. I guess this merits a new question…..

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement