Dialog Fails To Open

I have a vanilla blazor wasm project where I try to show a dialog immediately on navigating to a new page.

So I added the dialog to the Index page of the vanilla blazor template and it works fine.

When I added the same logic to the counter page, I see the open then immediate close event fire. What's wrong here?

I do notice that if I refresh on the Counter page it then works.

Program.cs

...
builder.Services.AddScoped<DialogService>();
...

MainLayout

@inherits LayoutComponentBase
<RadzenDialog />
<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

Vanilla Index Page

@page "/"

<PageTitle>Index</PageTitle>
<Spinner Show=@true />
<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

Vanilla Counter Page

@page "/counter"

<PageTitle>Counter</PageTitle>
<Spinner Show=@true />
<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

Spinner Component (just a dialog component)

@namespace RadzenExample.Client.Components
@implements IDisposable
@inject DialogService _dialogService;

@code {

    [Parameter]
    public required bool Show { get; set; }

    protected override async Task OnInitializedAsync()
    {
        _dialogService.OnOpen += Open;
        _dialogService.OnClose += Close;

        if (Show)
        {
            await ShowDialogAsync();
        }
        else
        {
            _dialogService.Close();
        }
    }

    private void Open(string s, Type t, Dictionary<string, object> d, DialogOptions o)
    {
        Console.WriteLine("open");
    }

    private void Close(dynamic d)
    {
        Console.WriteLine("close");
    }

    private async Task ShowDialogAsync()
    {
        await _dialogService.OpenAsync(string.Empty,
            s => @<div>Click ESC to close</div>,
new DialogOptions()
{
    ShowTitle = false,
    Style = "min-height:auto;min-width:auto;width:auto",
    CloseDialogOnEsc = true
}
);
    }

    void IDisposable.Dispose()
    {
        _dialogService.OnOpen -= Open;
        _dialogService.OnClose -= Close;
    }
}

Make sure you follow the instructions on our getting started page: RadzenDialog should be declared in your layout and interactivity should be enabled for your application.

Yes, I have registered dependency injected the DialogService in Program.cs and added the RadzenDialog on the MainLayout.

Like I said it works on the Index page. But not when I click the Counter link and it loads the counter page it does NOT work.

@ enchev Here is a simple example that demonstrates the above issue.

The app loads and the Index pages opens the dialog immediately as expected.

Press ESC to close it.

Click the Counter link in the left navigation, no dialog opens... that is the issue.

If you refresh the Counter page it will open however.

RadzenExample.zip (718.3 KB)

If you place await Task.Yield(); just before opening the dialog everything will start to work magically:


dialog

1 Like

Do you have an explanation why that is necessary? @enchev

If you look at the DialogService code you will notice that all dialogs are closed in case of navigation:

Task.Yeld() will give the UI time to render and will not raise the navigation event after you open the dialog but before that.

1 Like

Thank you for that. Makes sense. Appreciate the follow up.