示例#1
0
        private async Task SayText()
        {
            try // error handling
            {
                // Disable the ListBoxes to prevent exceptions
                VoicesListBox.IsEnabled    = false;
                LanguagesListBox.IsEnabled = false;

                // Stop playing any Synthesized speech
                _speechSynthesizer.CancelAll();

                // Check that string is not null or empty
                if (!string.IsNullOrEmpty(PhraseTextBox.Text))
                {
                    // Call SpeakTextAsync and await
                    await _speechSynthesizer.SpeakTextAsync(PhraseTextBox.Text);

                    MessageBox.Show("Text read: '" + PhraseTextBox.Text + "'");
                }
                else // if string is null or empty, display error message
                {
                    MessageBox.Show("Phrase text is required.",
                                    "Error",
                                    MessageBoxButton.OK);
                }

                // Re-enable the ListBoxes
                VoicesListBox.IsEnabled    = true;
                LanguagesListBox.IsEnabled = true;
            }
            catch (Exception ex) // Catch exception and call function to handle it.
            {
                HandleSpeechSynthesisError(ex);
            }
        }
示例#2
0
        private async Task SayText(string text)
        {
            try // error handling
            {
                // Stop playing any Synthesized speech
                _speechSynthesizer.CancelAll();

                // Check that string is not null or empty
                if (!string.IsNullOrEmpty(text))
                {
                    // Call SpeakTextAsync and await
                    await _speechSynthesizer.SpeakTextAsync(text);
                }
                else // if string is null or empty, display error message
                {
                    MessageBox.Show("Phrase text is required.",
                                    "Error",
                                    MessageBoxButton.OK);
                }
            }
            catch (Exception ex) // Catch exception and call function to handle it.
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#3
0
        private async void SayItButtonClick(object sender, RoutedEventArgs e)
        {
            try // Basic error handling
            {
                // Disable the ListBoxes to prevent exceptions
                VoicesListBox.IsEnabled    = false;
                LanguagesListBox.IsEnabled = false;

                // Stop playing any Synthesized speech
                _speechSynthesizer.CancelAll();

                // Check that string is not null or empty
                if (!string.IsNullOrEmpty(PhraseTextBox.Text))
                {
                    // Call SpeakTextAsync and await
                    await _speechSynthesizer.SpeakTextAsync(PhraseTextBox.Text);

                    MessageBox.Show("Text read: '" + PhraseTextBox.Text + "'");
                }
                else // if string is null or empty, display error message
                {
                    MessageBox.Show("Phrase text is required.",
                                    "Error",
                                    MessageBoxButton.OK);
                }

                // Re-enable the ListBoxes
                VoicesListBox.IsEnabled    = true;
                LanguagesListBox.IsEnabled = true;
            }
            catch (Exception ex) // Catch exceptions and display in MessageBox
            {
                MessageBox.Show(ex.Message,
                                "Exception",
                                MessageBoxButton.OK);
            }
        }