From 26bf09d5baafd395bdede863550e8e58139c0c54 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 17 Mar 2026 08:37:30 +0000 Subject: [PATCH 1/2] Initial plan From a5ded68fd3cec23f9acac82e6514b571c03cc9cd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 17 Mar 2026 08:40:46 +0000 Subject: [PATCH 2/2] docs: add CancellationToken explanation and fix undefined variable in SpeechToText docs Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com> --- docs/maui/essentials/speech-to-text.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/maui/essentials/speech-to-text.md b/docs/maui/essentials/speech-to-text.md index 18aaa0d8..6a7d6152 100644 --- a/docs/maui/essentials/speech-to-text.md +++ b/docs/maui/essentials/speech-to-text.md @@ -59,6 +59,9 @@ Add permissions to `tizen-manifest.xml`: The `SpeechToText` can be added to a .NET MAUI application in the following way. +> [!NOTE] +> The methods in this API accept a `CancellationToken` to cancel asynchronous operations. A `CancellationToken` can be obtained by creating a [`CancellationTokenSource`](/dotnet/api/system.threading.cancellationtokensource) and accessing its `.Token` property. You can also use `CancellationToken.None` if you don't need to cancel the operation. For more detail on how to provide a `CancellationToken` refer to the [Microsoft documentation](/dotnet/standard/threading/cancellation-in-managed-threads). + ### Request permissions Developers must manually request Permissions.Microphone and also call ISpeechToText.RequestPermissions(): @@ -197,6 +200,7 @@ Now you can inject the service like this: public partial class MainPage : ContentPage { private readonly ISpeechToText speechToText; + private CancellationTokenSource? cancellationTokenSource; public MainPage(ISpeechToText speechToText) { @@ -206,14 +210,17 @@ public partial class MainPage : ContentPage public async void Listen(object sender, EventArgs args) { - var isGranted = await speechToText.RequestPermissions(cancellationToken); + cancellationTokenSource?.Cancel(); + cancellationTokenSource = new CancellationTokenSource(); + + var isGranted = await speechToText.RequestPermissions(cancellationTokenSource.Token); if (!isGranted) { await Toast.Make("Permission not granted").Show(CancellationToken.None); return; } - await speechToText.StartListenAsync(new SpeechToTextOptions { Culture = CultureInfo.CurrentCulture, ShouldReportPartialResults = true }, CancellationToken.None); + await speechToText.StartListenAsync(new SpeechToTextOptions { Culture = CultureInfo.CurrentCulture, ShouldReportPartialResults = true }, cancellationTokenSource.Token); } } ```