/// <summary> /// 重写以设置 Ioc 容器。 /// </summary> protected override void Configure() { var catalog = new AggregateCatalog ( AssemblySource.Instance .Select(x => new AssemblyCatalog(x)) .OfType <ComposablePartCatalog>() ); //添加MEF自动发现包含的程序集 catalog.Catalogs.Add(new AssemblyCatalog(typeof(ILogger).Assembly)); catalog.Catalogs.Add(new AssemblyCatalog(typeof(IThemeManager).Assembly)); catalog.Catalogs.Add(new AssemblyCatalog(typeof(ITranslatorEngine).Assembly)); var batch = new CompositionBatch(); _container = new CompositionContainer(catalog); batch.AddExportedValue(_container); //注入控制反转 _windowManager = new WindowManager(); batch.AddExportedValue(_windowManager); _eventAggregator = new EventAggregator(); batch.AddExportedValue(_eventAggregator); _logger = _container.GetExportedValue <ILogger>(); batch.AddExportedValue(_logger); _themeManager = _container.GetExportedValue <IThemeManager>(); batch.AddExportedValue(_themeManager); _translatorEngine = _container.GetExportedValue <ITranslatorEngine>(); batch.AddExportedValue(_translatorEngine); _container.Compose(batch); }
private void TranslateButton_Click(object sender, EventArgs e) { this.UseWaitCursor = true; try { if (!running) { var sourceLanguage = (string)comboBox1.SelectedValue; var targetLanguage = (string)comboBox2.SelectedValue; var values = GetValuesForTranslation(this.context.CurrentBaseResourceSet).ToList(); // filter out already translated items, unless opposite is selected by end-user var translateAll = (int)comboBox3.SelectedValue == 0; var filteredValues = from p in values where !this.context.CurrentLocalResourceSet.HasValue(p.Text) || translateAll select p; // invoke 'before translation hook' to allow other plugins to tamper with the translation foreach (var translationItem in filteredValues) { this.webTranslatorPlugIn.InvokeBeforeItemAutoTranslation(translationItem); } translator = this.context.Container.Resolve <ITranslatorEngine>(); translator.TranslationReceived += this.translator_TranslationReceived; this.running = true; this.RefreshStatus(); translator.BeginTranslate(values, sourceLanguage, targetLanguage, this.TranslationComplete, null); } else { this.translator.Stop(); } } catch (Exception exception) { Tools.ShowExceptionMessageBox(exception); } finally { this.UseWaitCursor = false; } }
private void TranslateButton_Click(object sender, EventArgs e) { this.UseWaitCursor = true; try { if (!running) { var sourceLanguage = (string)comboBox1.SelectedValue; var targetLanguage = (string)comboBox2.SelectedValue; var values = GetValuesForTranslation(this.context.CurrentBaseResourceSet).ToList(); // filter out already translated items, unless opposite is selected by end-user var translateAll = (int)comboBox3.SelectedValue == 0; var filteredValues = from p in values where !this.context.CurrentLocalResourceSet.HasValue(p.Text) || translateAll select p; // invoke 'before translation hook' to allow other plugins to tamper with the translation foreach (var translationItem in filteredValues) { this.webTranslatorPlugIn.InvokeBeforeItemAutoTranslation(translationItem); } translator = this.context.Container.Resolve<ITranslatorEngine>(); translator.TranslationReceived += this.translator_TranslationReceived; this.running = true; this.RefreshStatus(); translator.BeginTranslate(values, sourceLanguage, targetLanguage, this.TranslationComplete, null); } else { this.translator.Stop(); } } catch (Exception exception) { Tools.ShowExceptionMessageBox(exception); } finally { this.UseWaitCursor = false; } }