# .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:

```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:

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

**3.** Define the `ViewModel` with `SpeechRecognizedCommand` and `ErrorOccurredCommand` definitions:

```C#
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](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and go to the **SpeechToTextButton > Features** category.

## See Also

* [Set Visual States](https://www.telerik.com/maui-ui/documentation/controls/speechtotextbutton/visual-states)
* [Events](https://www.telerik.com/maui-ui/documentation/controls/speechtotextbutton/events)
* [Custom Recognizer](https://www.telerik.com/maui-ui/documentation/controls/speechtotextbutton/custom-recognizer)
* [Style the SpeechToTextButton](https://www.telerik.com/maui-ui/documentation/controls/speechtotextbutton/styling)
