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;
}
}