What I am trying to do is to alert the user when the Blazor Application is running in their browser, and this is not working. Stepping through the code, it is executing the code, but not working until you come back to the page.
After adding a new Blazor Wasm application, installing this library, and then updating the "Index.razor" page to:
@using Toolbelt.Blazor.SpeechSynthesis @{/* Add these two lines. */}
@inject SpeechSynthesis SpeechSynthesis
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
@code {
string Text1 = "Hello From the Blazor application's Home page";
string Text2 = "Welcome back to the Blazor application's Home page";
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
this.SpeechSynthesis.Speak(this.Text1); // <-- Speak!
this.SpeechSynthesis.Speak(this.Text1); // <-- Speak!
this.SpeechSynthesis.Speak(this.Text1); // <-- Speak!
}
else
{
this.SpeechSynthesis.Speak(this.Text2); // <-- Speak!
}
}
}
You can only here the message when you come back to the page. In addition, "firstRender" seems to be true when going back to the page. Likewise if you refresh the page, you get the same behavior. So, this may be a Blazor issue or both.