Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IUnityContainer container)
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
            /// 初始化翻译类
            _translator = new Translator();
            #region 初始化语音识别客户端

            _ttsClient = new TtsClient();

            #endregion
            //初始化录音对象
            Recorder          = new Record(AppDomain.CurrentDomain.BaseDirectory + @"\RecordCache", SynchronizationContext.Current);
            Recorder.Success += Recorder_Success;
            #region 初始化定时器
            _timer          = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick    += _timer_Tick;
            #endregion
            //初始化设置界面
            SettingView          = container.Resolve <SettingView>();
            _changeCaptureWindow = container.Resolve <ChangeCaptureView>();
            #region 初始化命令

            SpeakCommand       = new RelayCommand <string>(SpeakCommandExcute);
            StartRecordCommand = new RelayCommand <MouseButtonEventArgs>((e) =>
            {
                Recorder.Start();
                _timer.Start();
                TextVisibility  = Visibility.Collapsed;
                VolumVisibility = Visibility.Visible;
            });
            EndRecordCommand = new RelayCommand <MouseButtonEventArgs>((e) =>
            {
                _timer.Stop();
                if (_recordTime > 0.5)
                {
                    IsBusyVisibility = Visibility.Visible;
                }
                Recorder.Stop();

                TextVisibility  = Visibility.Visible;
                VolumVisibility = Visibility.Collapsed;
            });
            TranslateCommand = new RelayCommand(TranslateCommandExcute);

            InputKeyUpCommand = new RelayCommand <KeyEventArgs>((e) =>
            {
                if (e.Key == Key.Enter)
                {
                    //执行翻译
                    TranslateCommandExcute();
                }
            });
            ChangeDeviceCommand = new RelayCommand(() =>
            {
                _changeCaptureWindow.ShowChangeDeviceWindow(Recorder);
            });
            #endregion
        }