private async void OnDataContextChanged(object?sender, EventArgs args) { _viewModel = (OpenDocumentViewModel)DataContext; if (_viewModel == null) { return; } _viewModel.NuGet.PackageInstalled += NuGetOnPackageInstalled; _viewModel.EditorFocus += (o, e) => _editor.Focus(); _viewModel.MainViewModel.EditorFontSizeChanged += size => _editor.FontSize = size; _editor.FontSize = _viewModel.MainViewModel.EditorFontSize; var documentText = await _viewModel.LoadText().ConfigureAwait(true); var documentId = _editor.Initialize(_viewModel.MainViewModel.RoslynHost, new ClassificationHighlightColors(), _viewModel.WorkingDirectory, documentText); _viewModel.Initialize(documentId, OnError, () => new TextSpan(_editor.SelectionStart, _editor.SelectionLength), this); _editor.Document.TextChanged += (o, e) => _viewModel.OnTextChanged(); }
public static void InitEditor() { lock (initLocked) { if (isInited) { return; } isInited = true; InitScriptOptions(); RoslynHostReferences roslynHostReferences; if (scriptOptions == null) { roslynHostReferences = RoslynHostReferences.NamespaceDefault.With( assemblyReferences: new[] { Assembly.GetExecutingAssembly(), Assembly.GetCallingAssembly(), typeof(MainController).Assembly, typeof(object).Assembly }); } else { roslynHostReferences = RoslynHostReferences.NamespaceDefault.With( references: scriptOptions.MetadataReferences.Where(a => !(a is UnresolvedMetadataReference)), imports: scriptOptions.Imports, assemblyReferences: new[] { Assembly.GetExecutingAssembly(), Assembly.GetCallingAssembly(), typeof(MainController).Assembly, typeof(object).Assembly }); } roslynHost = new CustomHost( globalType, additionalAssemblies: new[] { Assembly.Load("RoslynPad.Roslyn.Windows"), Assembly.Load("RoslynPad.Editor.Windows") }, references: roslynHostReferences); if (instance == null) { CreateEditorInstance(); } instance.Invoke((Action)(() => { roslynCodeEditor = new RoslynCodeEditor { FontFamily = new System.Windows.Media.FontFamily("Consolas") }; docId = roslynCodeEditor.Initialize(roslynHost, new ClassificationHighlightColors(), Directory.GetCurrentDirectory(), string.Empty); if (!Directory.Exists("Code")) { Directory.CreateDirectory("Code"); } if (File.Exists("Code\\_last")) { roslynCodeEditor.Text = File.ReadAllText("Code\\_last"); } instance.elementHost1.Child = roslynCodeEditor; foreach (var item in Directory.EnumerateFiles("Code")) { instance.comboBox1.Items.Add(item.Substring(5)); } instance.comboBox1.SelectedIndexChanged += (_, __) => { if (File.Exists($"Code\\{instance.comboBox1.Text}")) { Code = File.ReadAllText($"Code\\{instance.comboBox1.Text}"); } }; //AppDomain.CurrentDomain.AssemblyResolve += (a, b) => //{ // var asms = AppDomain.CurrentDomain.GetAssemblies(); // var fasms = asms.Where(a => a.FullName == b.Name).ToArray(); // if (fasms.Length > 0) return fasms[0]; // else // { // string asmName; // if (b.Name.IndexOf(',') > 0) // asmName = b.Name.Substring(0, b.Name.IndexOf(',')); // else asmName = b.Name; // var dinfo = Directory.CreateDirectory("Plugins"); // foreach (var item in dinfo.EnumerateFiles("*.dll", SearchOption.AllDirectories)) // { // if (item.Name == asmName) // { // return assemblyLoadContext.LoadFromAssemblyPath(item.FullName); // } // } // } // return null; //}; })); } }