Is there a way to stop in a Blazor component method until a boolean is set?

David Thielen 3,211 Reputation points
2023-04-25T00:29:10.0766667+00:00

This is for Blazor Server version 7. I have a situation where the user is deciding to navigate to another page on the site when they have entered fields on a form and have not saved them. I am using <NavigationLock ConfirmExternalNavigation="@_hasUnsavedChanges" OnBeforeInternalNavigation="OnBeforeInternalNavigation" /> to ask the user "Are you sure?" in OnBeforeInternalNavigation(). In that method I display a popup component. Once it is displayed, I want to stop execution in that method until the user clicks the Yes or No button. Once that is clicked I want to then resume execution in that method , calling context.PreventNavigation() if they clicked No. First off, is this a bad idea? The page logically is synchronous at this point where the only thing they can do on the page is click yes or no. Second, how can I do this? Are events the best approach? Semaphores? thanks - dave

Developer technologies | .NET | Blazor
{count} vote

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2023-04-25T03:02:52.9266667+00:00

    As html and JavaScript calls (required to create the pop up) are async, you can not make the code sync. Also the virtual dom render would be blocked.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.