简体   繁体   English

C#语音合成问题

[英]C# Speech Synthesis issue

I have run into an issue that has thrown me for a loop since last night. 自昨晚以来,我遇到了一个使我陷入困境的问题。 I decided that in order for my program to be as user friendly as possible, I should do some exception handling so a user would know why it isn't working. 我决定为了使程序尽可能对用户友好,我应该进行一些异常处理,以便用户知道为什么它不起作用。 However, No matter how I tried to catch the System.Argument Exception, it still would be thrown. 但是,无论我如何尝试捕获System.Argument异常,它仍然会被抛出。 I'm not looking for someone to just give me some code to fix this, I would really like someone to explain why this is happening so I know what to do to handle this sort of problem in the future. 我并不是在找人给我一些代码来解决这个问题,我真的很想找一个人解释为什么会这样,所以我知道将来要做什么来解决这种问题。

using System.Speech.Synthesis;

namespace SpeechProject
{
    /// <summary>
    /// Interaction logic for EnglishChinese.xaml
    /// </summary>
    public partial class EnglishChinese : Page
    {
        public EnglishChinese()
        {
            InitializeComponent();
        }

        private void speakBtn_Click(object sender, RoutedEventArgs e)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();

            try
            {
                synth.SelectVoice("Microsoft Hanhan Desktop");
                synth.Speak(spokenWords.Text);
            }

            catch (ArgumentException)
            {
                MessageBox.Show("You need to install the China Chinese Simplified Language Pack to use this feature");
            }
        }
    }
}

I then tried a different approach from this one. 然后,我尝试了与此方法不同的方法。 I tried an if else combination. 我尝试了if if else组合。 Although, i'm pretty sure i'm not doing something correctly with that said if statement. 虽然,我很确定我没有正确地执行if语句。 But it does however work for using "Microsoft Zira Desktop" but does not work for the others. 但是,它确实适用于使用“ Microsoft Zira Desktop”,但不适用于其他产品。

 public partial class EnglishChinese : Page
    {
        public EnglishChinese()
        {
            InitializeComponent();
        }

        private void speakBtn_Click(object sender, RoutedEventArgs e)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();

            if (synth.Voice.Name == "Microsoft Huihui Desktop")
            {
                synth.SelectVoice("Microsoft Huihui Desktop");
                synth.Speak(spokenWords.Text);
            }

            else
            {
                MessageBox.Show("You need to install the China Chinese Simplified Language Pack to use this feature");
            }
        }
    }
}

// It automatically throws the message box, when I click the button //当我点击按钮时,它会自动引发消息框

This is rather confusing since if it works for one, it should work for all, but that isn't the case with this if statement structure. 这相当令人困惑,因为如果它适用于一个,它应该适用于所有人,但是这种if语句结构不是这种情况。 Any tips would be very much appreciated. 任何提示将不胜感激。

Results with the catch Exception ex and Argument Exception ext: catch Exception ex和Argument Exception ext的结果: 在此处输入图片说明

Stewart_R, Psudonym and EBrown all helped answer this question. Stewart_R,Psudonym和EBrown都帮助回答了这个问题。 Debug settings needed to be changed to reflect how I wanted certain exceptions to be handled. 需要更改调试设置,以反映我希望如何处理某些异常。 And the Try Catch method was the better method to use instead of the If Else statements. 而Try Catch方法是更好的方法,而不是If Else语句。 I had to make sure I caught the specific exception I wanted, in this case it was ArgumentException. 我必须确保捕获到了想要的特定异常,在这种情况下是ArgumentException。 Thank you guys for your help. 谢谢你们的帮助。 I really appreciate it. 我真的很感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM