Blazor client-side debugging

For those who like some pictures, here’s a step by step using Visual Studio 16.4 preview (.NET Core 3.1 preview 2) & Chrome version 78. Start up the app using a debug profile. E.g. After site loads, and with the cursor focus on the chrome tab press “Shift+Alt+D”. Chrome will open a new tab showing … Read more

Blazor, how can I trigger the enter key event to action a button function?

onkeypress is fired only for character keys. onkeydown will fire for all keys pressed. I found some explanation of differences between all key events here Try it with onkeydown and it worked: <input type=”text” @onkeydown=”@Enter” /> In the event handler you will have to do this (notice that I check for both Enter and NumpadEnter … Read more

Blazor redirect to login if user is not authenticated

There may be slicker ways of doing this, but this is what worked for me: Assuming you’ve configured Authentication correctly according to these instructions In your MainLayout.razor (which is used by default for all components) add a code block as follows: @inject NavigationManager NavigationManager @code{ [CascadingParameter] protected Task<AuthenticationState> AuthStat { get; set; } protected async … Read more

StateHasChanged() vs InvokeAsync(StateHasChanged) in Blazor

I have tried calling StateHasChanged() – instead of InvokeAsync(StateHasChanged) – in a Timer’s Elapsed event, and it works as expected That must have been on WebAssembly. When you try that on Blazor Serverside I would expect an exception. StateHasChanged() checks if it runs on the right thread. The core issue is that the rendering and … Read more

When should I call StateHasChanged and when Blazor automatically intercepts that something is changed?

Generally speaking, the StateHasChanged() method is automatically called after a UI event is triggered, as for instance, after clicking a button element, the click event is raised, and the StateHasChanged() method is automatically called to notify the component that its state has changed and it should re-render. When the Index component is initially accessed. The … Read more

Is there an equivalent to Html.Raw in Blazor?

Feature to render raw HTML was added in Blazor 0.5.0 version. This is the example of how raw HTML can be rendered from string containing HTML content: @((MarkupString)myMarkup) @functions { string myMarkup = “<p class=”markup”>This is a <em>markup string</em>.</p>”; } More info can be found in “Blazor 0.5.0 experimental release now available” announcement.

tech