コード例 #1
0
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                TbxMinValue.Focus();

                _recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("zh-CN"));
                _recognizer.LoadGrammar(new DictationGrammar());
                _recognizer.SpeechRecognized += new EventHandler <SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
                _recognizer.SetInputToDefaultAudioDevice();
            }
            catch (Exception ex)
            {
                ShowMessage("加载异常:" + ex.Message);
            }
        }
コード例 #2
0
        private void BtnStartPlay_OnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var strMin = TbxMinValue.Text;
                int min;
                if (string.IsNullOrEmpty((strMin)) || !int.TryParse(strMin, out min))
                {
                    ShowMessage("请输入正确的最小值");
                    TbxMinValue.Focus();
                    return;
                }

                var strMax = TbxMaxValue.Text;
                int max;
                if (string.IsNullOrEmpty((strMax)) || !int.TryParse(strMax, out max))
                {
                    ShowMessage("请输入正确的最大值");
                    TbxMinValue.Focus();
                    return;
                }

                var strMaxTryCount = TbxMaxTryCount.Text;
                int maxTryCount    = 0;

                if (!string.IsNullOrEmpty((strMaxTryCount)) && !int.TryParse(strMaxTryCount, out maxTryCount))
                {
                    ShowMessage("请输入正确的最大次数");
                    TbxMaxTryCount.Focus();
                    return;
                }

                GenerateRandomNumber(min, max);
                _maxTryCount         = maxTryCount;
                _tryCount            = 0;
                PanelInput.IsEnabled = true;
                TbxInput.Focus();
                PanelStartPlay.IsEnabled = false;
                _startTime = DateTime.Now;
                _recognizer.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception ex)
            {
                ShowMessage("操作异常:" + ex);
            }
        }