| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Windows;
- using CodeForge;
- using Microsoft.Win32;
- namespace Plugins.Script.Views
- {
- /// <summary>
- /// 程序集引用管理对话框(照 CodeForge Demo 的引用管理器做法):ListBox + 添加 DLL / 移除 / 关闭。
- /// 全部走 CodeEditorControl 的字符串 API(GetReferenceNames / InstallAndReferenceDll / RemoveReferenceByName),
- /// 添加的 DLL 由控件自动归档到运行目录 References\,编辑器补全与流程运行期(ScriptCompiler 扫描同目录)共用。
- /// </summary>
- public partial class ScriptReferenceDialog : Window
- {
- private readonly CodeEditorControl _editor;
- public ScriptReferenceDialog(CodeEditorControl editor, Window owner)
- {
- _editor = editor;
- InitializeComponent();
- if (owner != null) Owner = owner;
- Reload();
- }
- private void Reload()
- {
- var names = new List<string>();
- try
- {
- foreach (var n in _editor.GetReferenceNames())
- {
- if (!string.IsNullOrEmpty(n) && !names.Contains(n)) names.Add(n);
- }
- }
- catch { /* 取引用名失败则显示空列表 */ }
- names.Sort(StringComparer.OrdinalIgnoreCase);
- RefsList.ItemsSource = names;
- }
- private void BtnAdd_Click(object sender, RoutedEventArgs e)
- {
- var dlg = new OpenFileDialog
- {
- Title = "添加引用(选择 .NET 程序集)— 会自动归档到运行目录 References\\",
- Filter = ".NET 程序集 (*.dll, *.exe)|*.dll;*.exe|所有文件 (*.*)|*.*",
- Multiselect = true
- };
- try
- {
- var refsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Runtime\\References");
- if (Directory.Exists(refsDir)) dlg.InitialDirectory = refsDir;
- }
- catch { }
- if (dlg.ShowDialog(this) != true) return;
- foreach (var path in dlg.FileNames)
- {
- bool ok = false;
- try { ok = _editor.InstallAndReferenceDll(path); }
- catch (Exception ex)
- {
- MessageBox.Show(this, "添加异常:" + Path.GetFileName(path) + "\n" + ex.Message,
- "程序集引用管理", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- if (!ok)
- {
- MessageBox.Show(this,
- "添加失败:" + Path.GetFileName(path) + "\n(该文件未进入 References\\,不会被引用;详见主界面输出)",
- "程序集引用管理", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- }
- Reload();
- }
- private void BtnRemove_Click(object sender, RoutedEventArgs e)
- {
- var sel = RefsList.SelectedItem as string;
- if (string.IsNullOrEmpty(sel))
- {
- MessageBox.Show(this, "请先选中列表中要移除的引用", "程序集引用管理",
- MessageBoxButton.OK, MessageBoxImage.Information);
- return;
- }
- try { _editor.RemoveReferenceByName(sel); }
- catch (Exception ex)
- {
- MessageBox.Show(this, "移除失败:" + sel + "\n" + ex.Message, "程序集引用管理",
- MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- Reload();
- }
- private void BtnClose_Click(object sender, RoutedEventArgs e) { Close(); }
- }
- }
|