/* * ExportInkMenuItem.cs * "导出墨迹" 菜单项 * 所属项目:Better-Seewo * 作者:雾启工作室 * * 说明: * 实现白板编辑菜单中的"导出墨迹"功能。 * 点击后将当前白板的墨迹数据导出为 .ink.json 格式的 JSON 文件。 * 默认文件名为"课件墨迹.ink.json"。 */ using System; using System.IO; using System.Threading.Tasks; using System.Windows; using dotnetCampus.EasiPlugin; using Newtonsoft.Json; namespace Better_Seewo.Better_EN5.UI { /// /// 导出墨迹菜单项 /// 实现 IMenuPluginItem 接口,在白板编辑菜单中显示"导出墨迹"按钮 /// 点击后打开文件保存对话框,将当前白板墨迹序列化为 JSON 文件 /// public class ExportInkMenuItem : IMenuPluginItem { /// /// 菜单项唯一标识符 /// public Guid Id { get; } = new Guid("A1B2C3D4-E5F6-7890-ABCD-EF1234567890"); /// /// 菜单项显示标题 /// 根据当前语言环境显示"导出墨迹"或"Export Ink" /// public string Title { get; } /// /// 菜单项排序提示值 /// 值越大越靠前,998 表示在菜单中靠前的位置 /// public int SortHint { get; } = 998; /// /// 语言资源源,用于获取多语言文本 /// private readonly DictionaryLanguageSource _languageSource; /// /// 构造函数 /// /// 中英文双语资源字典 public ExportInkMenuItem(DictionaryLanguageSource languageSource) { _languageSource = languageSource; // 从语言资源中获取菜单项标题,默认中文 Title = _languageSource.GetString("ExportInk.Title") ?? "导出墨迹"; } /// /// 获取菜单项图标 /// 返回菜单项在工具栏中显示的图标资源路径 /// /// 图标资源路径字符串 public string GetMenuIcon() { // 返回导出图标路径 return "pack://application:,,,/Better-EN5;component/Resources/export_icon.png"; } /// /// 菜单项点击事件处理 /// 打开文件保存对话框,将当前白板墨迹导出为 .ink.json 文件 /// public async Task OnClickAsync() { await Task.Run(() => { // 从白板获取当前墨迹数据 var inkData = InkDataHelper.ExportInkData(); if (inkData == null) { MessageBox.Show("当前白板没有可导出的墨迹数据。", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } // 创建文件保存对话框 var saveFileDialog = new Microsoft.Win32.SaveFileDialog { // 默认文件名 FileName = "课件墨迹.ink.json", // 文件过滤器 Filter = "墨迹 JSON 文件 (*.ink.json)|*.ink.json|所有文件 (*.*)|*.*", // 默认扩展名 DefaultExt = ".ink.json", // 对话框标题 Title = "导出墨迹", }; // 显示保存对话框 if (saveFileDialog.ShowDialog() == true) { // 使用 Newtonsoft.Json 将墨迹数据序列化为格式化的 JSON 字符串 string json = JsonConvert.SerializeObject(inkData, Formatting.Indented); // 写入文件 File.WriteAllText(saveFileDialog.FileName, json); } }); } } }