Event arguments for the SpeechTextBox SpeechRecognized event.
Inheritance: System.EventArgs
        private void ReverseTextBox_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            SpeechRecognitionResult recoResult = e.Result;

            ReverseTextBox.Text = ReverseString(recoResult.Text);

            e.Canceled = true;
        }
Exemple #2
0
        /// <summary>
        /// Method to instantiate recognizer with appropriate grammar and perform recognition.
        /// </summary>
        private async void HandleSpeech()
        {
            if (_handlingSpeech)
            {
                return;
            }

            _handlingSpeech = true;
            try
            {
                SpeechRecognizerUI recognizer = new SpeechRecognizerUI();
                SpeechRecognitionUIResult result = null;

                if (this.InputScope != null && (this.InputScope.Names[0] as InputScopeName).NameValue.Equals(InputScopeNameValue.Search))
                {
                    recognizer.Recognizer.Grammars.AddGrammarFromPredefinedType("WebSearchGrammar", SpeechPredefinedGrammar.WebSearch);
                }

                try
                {
                    result = await recognizer.RecognizeWithUIAsync();
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    if ((uint)ex.HResult == 0x80045508)
                    {
                        // This can occur when speech recognition is interupted by navigation away from
                        // the app. We'll just swallow the exception to work around it.
                        return;
                    }

                    MessageBox.Show("An error occured. \n" + ex.Message);
                    return;
                }

                // The SpeechRecognizerUI component will handle cases where the speech was not recognized and prompt 
                // user to retry. This check is just to make sure that the speech recognition request was not 
                // canceled by the back button, navigation, etc.
                if (result.ResultStatus == SpeechRecognitionUIStatus.Succeeded)
                {
                    // Raise SpeechRecognized event
                    var handler = SpeechRecognized;
                    SpeechRecognizedEventArgs eventArgs = new SpeechRecognizedEventArgs(result.RecognitionResult);

                    if (handler != null)
                    {
                        handler(this, eventArgs);

                        if (eventArgs.Canceled)
                        {
                            return;
                        }
                    }

                    // Update display
                    string originalText = this.Text;

                    if (_useSelectedTextReplacement)
                    {
                        string newText = originalText.Substring(0, _selectionStart) + result.RecognitionResult.Text + originalText.Substring(_selectionEnd + 1);
                        this.Text = newText;
                        this.Select(_selectionStart, result.RecognitionResult.Text.Length);
                    }
                    else
                    {
                        this.Text = result.RecognitionResult.Text;
                        this.Focus();
                        this.Select(_selectionStart, result.RecognitionResult.Text.Length);
                    }
                }
            }
            finally
            {
                _handlingSpeech = false;
            }
        }