New to Telerik UI for .NET MAUI? Start a free 30-day trial

.NET MAUI SpeechToTextButton Commands

The .NET MAUI SpeechToTextButton provides commands that execute when specific actions occur, such as when speech is recognized or when an error occurs. This allows you to handle these events in a more MVVM-friendly way.

The .NET MAUI SpeechToTextButton exposes the following commands:

  • SpeechRecognizedCommand (ICommand)—Specifies a command to execute when the speech recognition is successful and the recognized text is available. The command context is SpeechToTextButtonSpeechRecognizedCommandContext, which contains the recognized text—FullText and confidence score—FullTextConfidenceScore.
  • ErrorOccurredCommand (ICommand)—Specifies a command to execute when an error occurs during the speech recognition process. The command context is SpeechToTextButtonErrorOccurredCommandContext, which contains the error message—Message and the exception—Exception.

Example

Here is an example using the SpeechRecognizedCommand and ErrorOccurredCommand:

1. Define the SpeechToTextButton in XAML:

<Grid ColumnDefinitions="*, Auto"
      RowDefinitions="Auto">
    <Editor x:Name="editor"
            AutoSize="TextChanges"
            Text="{Binding Text, Mode=TwoWay}" />
    <telerik:RadSpeechToTextButton x:Name="speechToTextButton"
                                   SpeechRecognizedCommand="{Binding SpeechRecognizedCommand}"
                                   ErrorOccurredCommand="{Binding ErrorOccurredCommand}"
                                   Grid.Column="1"
                                   VerticalOptions="Start" />
</Grid>

2. Add the telerik namespace:

xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"

3. Define the ViewModel with SpeechRecognizedCommand and ErrorOccurredCommand definitions:

public class ViewModel : NotifyPropertyChangedBase
{
    private string text;

    public ViewModel()
    {
        this.SpeechRecognizedCommand = new Command(this.OnSpeechRecognized);
        this.ErrorOccurredCommand = new Command(this.OnErrorOccurred);
    }

    public string Text
    {
        get => this.text;
        set => this.UpdateValue(ref this.text, value);
    }

    public ICommand SpeechRecognizedCommand { get; }

    public ICommand ErrorOccurredCommand { get; }

    private void OnSpeechRecognized(object obj)
    {
        SpeechToTextButtonSpeechRecognizedCommandContext context = (SpeechToTextButtonSpeechRecognizedCommandContext)obj;
        this.Text = context.FullText;
    }

    private void OnErrorOccurred(object obj)
    {
        SpeechToTextButtonErrorOccurredCommandContext context = (SpeechToTextButtonErrorOccurredCommandContext)obj;
        this.Text = $"Error: {context.Message}.\n{context.Exception}";
    }
}

For a runnable example with the SpeechToTextButton Commands scenario, see the SDKBrowser Demo Application and go to the SpeechToTextButton > Features category.

See Also

In this article