From 2fe17e7f6596b47b33a40d8733a0527ba5d9b2fb Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Mon, 19 Dec 2022 11:24:45 +0800 Subject: [PATCH] feat: add crosswordtranslate --- STranslate/MainWindow.xaml | 4 +- STranslate/MainWindow.xaml.cs | 219 ++++++++++++++++++++++----- STranslate/Model/HotKeys.cs | 33 ++++ STranslate/STranslate.csproj | 4 + STranslate/STranslate.yml | 6 +- STranslate/Utils/GetWordsUtil.cs | 53 +++++++ STranslate/Utils/HotKeysUtil2.cs | 94 ++++++++++++ STranslate/Utils/NativeMethod.cs | 252 +++++++++++++++++++++++++++++++ STranslate/ViewModel/MainVM.cs | 47 +++--- 9 files changed, 644 insertions(+), 68 deletions(-) create mode 100644 STranslate/Model/HotKeys.cs create mode 100644 STranslate/Utils/GetWordsUtil.cs create mode 100644 STranslate/Utils/HotKeysUtil2.cs create mode 100644 STranslate/Utils/NativeMethod.cs diff --git a/STranslate/MainWindow.xaml b/STranslate/MainWindow.xaml index 217cfa9..0a98fe5 100644 --- a/STranslate/MainWindow.xaml +++ b/STranslate/MainWindow.xaml @@ -30,7 +30,7 @@ - - + --> public partial class MainWindow : Window { + public MainWindow() + { + InitializeComponent(); + + InitHwnd(); + InitTray(); + + this.DataContext = MainVM.Instance; + } + + private void InitHwnd() + { + var helper = new WindowInteropHelper(this); + helper.EnsureHandle(); + } + /// - /// 监听全局快捷键 + /// 初始化托盘 /// - /// - protected override void OnSourceInitialized(EventArgs e) + private void InitTray() { - HotkeysUtil.InitialHook(this); - HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.A, () => - { - this.Show(); - this.Activate(); - this.TextBoxInput.Focus(); - }); -#if false - HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.D, () => - { - //复制内容 - KeyboardUtil.Press(Key.LeftCtrl); - KeyboardUtil.Type(Key.C); - KeyboardUtil.Release(Key.LeftCtrl); - System.Threading.Thread.Sleep(200); - System.Diagnostics.Debug.Print(Clipboard.GetText()); + //notifyIcon.BalloonTipText = "程序开始运行"; + //notifyIcon.ShowBalloonTip(1000); + notifyIcon.Text = "STranslate"; + notifyIcon.Icon = new System.Drawing.Icon(@"D:\CodePepo\STranslate\STranslate\Images\translate2.ico"); + notifyIcon.Visible = true; - //this.Show(); - //this.Activate(); - //this.TextBoxInput.Text = "123"; + System.Windows.Forms.MenuItem inputTranslateButton = new System.Windows.Forms.MenuItem("输入翻译"); + inputTranslateButton.Click += new EventHandler(InputTranslate_Click); - //this.TextBoxInput.Text = Clipboard.GetText(); - //this.TextBoxInput.Focus(); + System.Windows.Forms.MenuItem crosswordTranslateButton = new System.Windows.Forms.MenuItem("划词翻译"); + crosswordTranslateButton.Click += new EventHandler(CrosswordTranslate_Click); - //KeyboardUtil.Type(Key.Enter); - }); -#endif - } + System.Windows.Forms.MenuItem screenshotTranslationButton = new System.Windows.Forms.MenuItem("截图翻译"); + screenshotTranslationButton.Click += new EventHandler(ScreenshotTranslation_Click); - public MainWindow() + System.Windows.Forms.MenuItem openMainWinButton = new System.Windows.Forms.MenuItem("显示主窗口"); + openMainWinButton.Click += new EventHandler(OpenMainWin_Click); + + System.Windows.Forms.MenuItem exitButton = new System.Windows.Forms.MenuItem("退出"); + exitButton.Click += new EventHandler(Exit_Click); + + System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { + inputTranslateButton, + crosswordTranslateButton, + screenshotTranslationButton, + openMainWinButton, + exitButton + }; + notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen); + } + /// + /// 显示主窗口 + /// + /// + /// + private void OpenMainWin_Click(object sender, EventArgs e) { - InitializeComponent(); - this.TextBoxInput.Focus(); - this.DataContext = MainVM.Instance; + this.Show(); + this.Activate(); } /// @@ -62,9 +83,8 @@ namespace STranslate { DragMove(); } - /// - /// 快捷键 + /// 软件运行时快捷键 /// /// /// @@ -77,12 +97,12 @@ namespace STranslate MainVM.Instance.InputTxt = string.Empty; MainVM.Instance.OutputTxt = string.Empty; } - //退出 Ctrl+Q + //退出 Ctrl+Shift+Q if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift) && e.Key == Key.Q) { - Application.Current.Shutdown(); + Exit_Click(null, null); } #if false //置顶/取消置顶 Ctrl+T @@ -119,11 +139,71 @@ namespace STranslate } #endif } + /// + /// 监听全局快捷键 + /// + /// + protected override void OnSourceInitialized(EventArgs e) + { + IntPtr handle = new WindowInteropHelper(this).Handle; + HotKeysUtil2.RegisterHotKey(handle); - private void NotifyIcon_Click(object sender, RoutedEventArgs e) + HwndSource source = HwndSource.FromHwnd(handle); + source.AddHook(WndProc); + +#if false + HotkeysUtil.InitialHook(this); + HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.A, () => + { + this.Show(); + this.Activate(); + this.TextBoxInput.Focus(); + }); + HotkeysUtil.Regist(HotkeyModifiers.MOD_ALT, Key.D, () => + { + //复制内容 + KeyboardUtil.Press(Key.LeftCtrl); + KeyboardUtil.Type(Key.C); + KeyboardUtil.Release(Key.LeftCtrl); + System.Threading.Thread.Sleep(200); + System.Diagnostics.Debug.Print(Clipboard.GetText()); + + //this.Show(); + //this.Activate(); + //this.TextBoxInput.Text = "123"; + + //this.TextBoxInput.Text = Clipboard.GetText(); + //this.TextBoxInput.Focus(); + + //KeyboardUtil.Type(Key.Enter); + }); +#endif + } + /// + /// 热键的功能 + /// + /// + protected IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handle) { - this.Show(); - this.Activate(); + switch (msg) + { + case 0x0312: //这个是window消息定义的 注册的热键消息 + //Console.WriteLine(wParam.ToString()); + if (wParam.ToString().Equals(HotKeysUtil2.InputTranslateId + "")) + { + this.InputTranslate_Click(null, null); + } + else if (wParam.ToString().Equals(HotKeysUtil2.CrosswordTranslateId + "")) + { + this.CrosswordTranslate_Click(null, null); + } + else if (wParam.ToString().Equals(HotKeysUtil2.ScreenShotTranslateId + "")) + { + this.ScreenshotTranslation_Click(null, null); + } + break; + } + return IntPtr.Zero; } /// @@ -134,8 +214,69 @@ namespace STranslate private void Window_Deactivated(object sender, EventArgs e) { this.Hide(); + } + + /// + /// 截图翻译 + /// + /// + /// + private void ScreenshotTranslation_Click(object sender, EventArgs e) + { + MessageBox.Show("开发中"); + } + + /// + /// 划词翻译 + /// + /// + /// + private void CrosswordTranslate_Click(object sender, EventArgs e) + { + ClearTextBox(); + var sentence = GetWords.Get(); + this.Show(); + this.Activate(); + this.TextBoxInput.Text = sentence.Trim(); + _ = MainVM.Instance.Translate(); + } + + /// + /// 输入翻译 + /// + /// + /// + private void InputTranslate_Click(object sender, EventArgs e) + { + ClearTextBox(); + Show(); + Activate(); + TextBoxInput.Focus(); + } + + /// + /// 退出程序 + /// + /// + /// + private void Exit_Click(object sender, EventArgs e) + { + notifyIcon.Dispose(); + Environment.Exit(0); + } + + /// + /// 清空输入输出框 + /// + private void ClearTextBox() + { MainVM.Instance.InputTxt = string.Empty; MainVM.Instance.OutputTxt = string.Empty; + } + /// + /// 托盘图标 + /// + private System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon(); } } \ No newline at end of file diff --git a/STranslate/Model/HotKeys.cs b/STranslate/Model/HotKeys.cs new file mode 100644 index 0000000..9b51bef --- /dev/null +++ b/STranslate/Model/HotKeys.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STranslate.Model +{ + public static class HotKeys + { + public static class InputTranslate + { + public static byte Modifiers = 1; + public static int Key = 65; + public static String Text = "A"; + public static bool Conflict = false; + } + public static class CrosswordTranslate + { + public static byte Modifiers = 0; + public static int Key = 113; + public static String Text = "F2"; + public static bool Conflict = false; + } + public static class ScreenShotTranslate + { + public static byte Modifiers = 1; + public static int Key = 83; + public static String Text = "S"; + public static bool Conflict = false; + } + } +} diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index b0a8d97..ac86cf1 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -85,11 +85,15 @@ MSBuild:Compile Designer + + + + diff --git a/STranslate/STranslate.yml b/STranslate/STranslate.yml index fe786e6..8818743 100644 --- a/STranslate/STranslate.yml +++ b/STranslate/STranslate.yml @@ -1,6 +1,6 @@ service: baidu baidu: - appid: - secretKey: + appid: 20220608001242348 + secretKey: nN2CGpQCbTodUnKDW04k deepl: - url: \ No newline at end of file + url: http://127.0.0.1:8000/translate \ No newline at end of file diff --git a/STranslate/Utils/GetWordsUtil.cs b/STranslate/Utils/GetWordsUtil.cs new file mode 100644 index 0000000..f417f57 --- /dev/null +++ b/STranslate/Utils/GetWordsUtil.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; + +namespace STranslate.Utils +{ + public class GetWords + { + public static String Get() + { + SendCtrlC(); + Thread.Sleep(200); + return NativeMethod.GetText(); + } + + private static void SendCtrlC() + { + //IntPtr hWnd = GetForegroundWindow(); + //SetForegroundWindow(hWnd); + uint KEYEVENTF_KEYUP = 2; + NativeMethod.keybd_event(System.Windows.Forms.Keys.ControlKey, 0, 0, 0); + NativeMethod.keybd_event(System.Windows.Forms.Keys.C, 0, 0, 0); + NativeMethod.keybd_event(System.Windows.Forms.Keys.C, 0, KEYEVENTF_KEYUP, 0); + NativeMethod.keybd_event(System.Windows.Forms.Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);// 'Left Control Up + } + + private static String GetDataFromClipboard() + { + try + { + if (Clipboard.ContainsText()) //检查是否存在文本 + { + string res = Clipboard.GetText(); + if (!string.IsNullOrWhiteSpace(res)) + { + return res; + } + } + } + catch (Exception e) + { + Console.WriteLine(e); + return null; + } + return null; + } + + } +} diff --git a/STranslate/Utils/HotKeysUtil2.cs b/STranslate/Utils/HotKeysUtil2.cs new file mode 100644 index 0000000..ea55568 --- /dev/null +++ b/STranslate/Utils/HotKeysUtil2.cs @@ -0,0 +1,94 @@ +using STranslate.Model; +using System; + +namespace STranslate.Utils +{ + internal class HotKeysUtil2 + { + public static IntPtr mainFormHandle; + + public static int InputTranslateId = 854; + public static byte InputTranslateModifiers; + public static int InputTranslateKey; + public static int CrosswordTranslateId = 855; + public static byte CrosswordTranslateModifiers; + public static int CrosswordTranslateKey; + public static int ScreenShotTranslateId = 856; + public static byte ScreenShotTranslateModifiers; + public static int ScreenShotTranslateKey; + public static void RegisterHotKey(IntPtr mainFormHandle) + { + HotKeysUtil2.mainFormHandle = mainFormHandle; + + InputTranslateModifiers = HotKeys.InputTranslate.Modifiers; + InputTranslateKey = HotKeys.InputTranslate.Key; + CrosswordTranslateModifiers = HotKeys.CrosswordTranslate.Modifiers; + CrosswordTranslateKey = HotKeys.CrosswordTranslate.Key; + ScreenShotTranslateModifiers = HotKeys.ScreenShotTranslate.Modifiers; + ScreenShotTranslateKey = HotKeys.ScreenShotTranslate.Key; + + if (HotKeys.InputTranslate.Key != 0) + { + HotKeys.InputTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, InputTranslateId, HotKeys.InputTranslate.Modifiers, HotKeys.InputTranslate.Key); + } + if (HotKeys.CrosswordTranslate.Key != 0) + { + HotKeys.CrosswordTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, CrosswordTranslateId, HotKeys.CrosswordTranslate.Modifiers, HotKeys.CrosswordTranslate.Key); + } + if (HotKeys.ScreenShotTranslate.Key != 0) + { + HotKeys.ScreenShotTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, HotKeys.ScreenShotTranslate.Modifiers, HotKeys.ScreenShotTranslate.Key); + } + } + + public static void UnRegisterHotKey() + { + NativeMethod.UnregisterHotKey(mainFormHandle, InputTranslateId); + NativeMethod.UnregisterHotKey(mainFormHandle, CrosswordTranslateId); + NativeMethod.UnregisterHotKey(mainFormHandle, ScreenShotTranslateId); + } + + public static void ReRegisterHotKey() + { + if (HotKeys.InputTranslate.Key == 0) + { + NativeMethod.UnregisterHotKey(mainFormHandle, InputTranslateId); + } + else if (InputTranslateModifiers != HotKeys.InputTranslate.Modifiers || InputTranslateKey != HotKeys.InputTranslate.Key) + { + { + NativeMethod.UnregisterHotKey(mainFormHandle, InputTranslateId); + HotKeys.InputTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, InputTranslateId, HotKeys.InputTranslate.Modifiers, HotKeys.InputTranslate.Key); + } + } + InputTranslateModifiers = HotKeys.InputTranslate.Modifiers; + InputTranslateKey = HotKeys.InputTranslate.Key; + + if (HotKeys.CrosswordTranslate.Key == 0) + { + NativeMethod.UnregisterHotKey(mainFormHandle, CrosswordTranslateId); + } + else if (CrosswordTranslateModifiers != HotKeys.CrosswordTranslate.Modifiers || CrosswordTranslateKey != HotKeys.CrosswordTranslate.Key) + { + { + NativeMethod.UnregisterHotKey(mainFormHandle, CrosswordTranslateId); + HotKeys.CrosswordTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, CrosswordTranslateId, HotKeys.CrosswordTranslate.Modifiers, HotKeys.CrosswordTranslate.Key); + } + } + CrosswordTranslateModifiers = HotKeys.CrosswordTranslate.Modifiers; + CrosswordTranslateKey = HotKeys.CrosswordTranslate.Key; + + if (HotKeys.ScreenShotTranslate.Key == 0) + { + NativeMethod.UnregisterHotKey(mainFormHandle, ScreenShotTranslateId); + } + else if (ScreenShotTranslateModifiers != HotKeys.ScreenShotTranslate.Modifiers || ScreenShotTranslateKey != HotKeys.ScreenShotTranslate.Key) + { + NativeMethod.UnregisterHotKey(mainFormHandle, ScreenShotTranslateId); + HotKeys.ScreenShotTranslate.Conflict = !NativeMethod.RegisterHotKey(mainFormHandle, ScreenShotTranslateId, HotKeys.ScreenShotTranslate.Modifiers, HotKeys.ScreenShotTranslate.Key); + } + ScreenShotTranslateModifiers = HotKeys.ScreenShotTranslate.Modifiers; + ScreenShotTranslateKey = HotKeys.ScreenShotTranslate.Key; + } + } +} diff --git a/STranslate/Utils/NativeMethod.cs b/STranslate/Utils/NativeMethod.cs new file mode 100644 index 0000000..917ad4d --- /dev/null +++ b/STranslate/Utils/NativeMethod.cs @@ -0,0 +1,252 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace STranslate.Utils +{ + internal class NativeMethod + { + /// + /// 获取进程句柄 + /// + /// + /// + [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] + public static extern IntPtr GetModuleHandle(string lpModuleName); + + /// + /// 设置窗口在最前端 + /// + /// 窗口句柄 + /// + [DllImport("User32.dll")] + public static extern bool SetForegroundWindow(IntPtr hWnd); + + /// + /// 获取最前端的窗口 + /// + /// 窗口句柄 + [DllImport("user32.dll", CharSet = CharSet.Auto)] + public static extern IntPtr GetForegroundWindow(); + + /// + /// 模拟触发键盘的按键 + /// + /// 按下的键 + /// + /// 触发的方式,0按下,2抬起 + /// + [DllImport("user32.dll")] + public static extern void keybd_event(System.Windows.Forms.Keys vk, byte bScan, uint dwFlags, uint dwExtraInfo); + + /// + /// 注册全局热键 + /// + /// 要定义热键的窗口的句柄 + /// 定义热键ID(不能与其它ID重复,全局唯一) + /// 标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效 + /// 定义热键的内容 + /// 成功,返回值不为0,失败,返回值为0。要得到扩展错误信息,调用GetLastError + [DllImport("user32.dll", SetLastError = true)] + public static extern bool RegisterHotKey(IntPtr hWnd, int id, byte fsModifiers, int vk); + + /// + /// 取消注册全局热键 + /// + /// 要取消热键的窗口的句柄 + /// 要取消热键的ID + /// + [DllImport("user32.dll", SetLastError = true)] + public static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + /// + /// 加载鼠标样式从文件中 + /// + /// + /// + [DllImport("user32.dll")] + public static extern IntPtr LoadCursorFromFile(string fileName); + + /// + /// 设置鼠标样式 + /// + /// + /// + [DllImport("user32.dll")] + public static extern IntPtr SetCursor(IntPtr cursorHandle); + + /// + /// 销毁鼠标样式 + /// + /// + /// + [DllImport("user32.dll")] + public static extern uint DestroyCursor(IntPtr cursorHandle); + + /// + /// 隐藏焦点,隐藏光标闪烁 + /// + /// + /// + [DllImport("user32", EntryPoint = "HideCaret")] + public static extern bool HideCaret(IntPtr hWnd); + + [DllImport("kernel32.dll")] + public static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); + + /// + /// 打开剪切板 + /// + /// + /// + [DllImport("User32")] + internal static extern bool OpenClipboard(IntPtr hWndNewOwner); + + /// + /// 关闭剪切板 + /// + /// + [DllImport("User32")] + internal static extern bool CloseClipboard(); + + /// + /// 清空剪切板 + /// + /// + [DllImport("User32")] + internal static extern bool EmptyClipboard(); + + /// + /// 剪切板格式化的数据是否可用 + /// + /// + /// + [DllImport("User32")] + internal static extern bool IsClipboardFormatAvailable(int format); + + /// + /// 获取剪切板数据 + /// + /// + /// + [DllImport("User32")] + internal static extern IntPtr GetClipboardData(int uFormat); + + /// + /// 设置剪切板数据 + /// + /// + /// + /// + [DllImport("User32", CharSet = CharSet.Unicode)] + internal static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem); + + const int HORZRES = 8; + const int VERTRES = 10; + const int LOGPIXELSX = 88; + const int LOGPIXELSY = 90; + const int DESKTOPVERTRES = 117; + const int DESKTOPHORZRES = 118; + + [DllImport("user32.dll")] + static extern IntPtr GetDC(IntPtr ptr); + [DllImport("gdi32.dll")] + static extern int GetDeviceCaps( + IntPtr hdc, // handle to DC + int nIndex // index of capability + ); + [DllImport("user32.dll", EntryPoint = "ReleaseDC")] + static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); + + /// + /// 获取系统dpi + /// + /// + public static double GetDpi() + { + double dDpi = 1; + IntPtr desktopDc = GetDC(IntPtr.Zero); + float horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX); + float verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY); + int dpi = (int)(horizontalDPI + verticalDPI) / 2; + dDpi = 1 + ((dpi - 96) / 24) * 0.25; + if (dDpi < 1) + { + dDpi = 1; + } + ReleaseDC(IntPtr.Zero, desktopDc); + return dDpi; + } + + /// + /// 获取窗口标题 + /// + /// + [DllImport("user32", SetLastError = true)] + public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); + + /// + /// 获取类的名字 + /// + /// + [DllImport("user32.dll")] + public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount); + + /// + /// 根据坐标获取窗口句柄 + /// + /// + [DllImport("user32")] + public static extern IntPtr WindowFromPoint(System.Drawing.Point Point); + + /// + /// 窗口置顶与取消置顶 + /// + /// + [DllImport("user32.dll")] + public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags); + + /// + /// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值) + /// + [Flags()] + public enum KeyModifiers + { + None = 0, + Alt = 1, + Ctrl = 2, + Shift = 4, + WindowsKey = 8 + } + + #region Clipboard + internal static void SetText(string text) + { + if (!OpenClipboard(IntPtr.Zero)) + { + SetText(text); + return; + } + EmptyClipboard(); + SetClipboardData(13, Marshal.StringToHGlobalUni(text)); + CloseClipboard(); + } + + internal static string GetText() + { + string value = string.Empty; + OpenClipboard(IntPtr.Zero); + if (IsClipboardFormatAvailable(13)) + { + IntPtr ptr = GetClipboardData(13); + if (ptr != IntPtr.Zero) + { + value = Marshal.PtrToStringUni(ptr); + } + } + CloseClipboard(); + return value; + } + #endregion + } +} diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index 70a1fc7..6ad5123 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -3,6 +3,7 @@ using STranslate.Utils; using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using System.Windows; using System.Windows.Input; @@ -14,8 +15,6 @@ namespace STranslate.ViewModel public static ConfigModel config = new ConfigModel(); private static Dictionary LanguageEnumDict { get => TranslateUtil.GetEnumList(); } - private string Text; - public MainVM() { //初始化界面参数 @@ -27,7 +26,7 @@ namespace STranslate.ViewModel //TODO: fix no config config = ConfigUtil.ReadConfig(ConfigPath); - //手动复制翻译结果 + //复制翻译结果 CopyTranslateResultCmd = new RelayCommand((_) => { return string.IsNullOrEmpty(OutputTxt) ? false : true; @@ -42,32 +41,32 @@ namespace STranslate.ViewModel return string.IsNullOrEmpty(InputTxt) ? false : true; }, async (_) => { - try - { - Text = InputTxt; - - //清空输入框 - InputTxt = ""; - - OutputTxt = "翻译中..."; + await Translate(); + }); + } + public async Task Translate() + { + try + { + //清空输入框 + OutputTxt = "翻译中..."; - //获取结果 - //var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, Text, LanguageEnum.EN, LanguageEnum.AUTO); + //获取结果 + //var translateResp = await TranslateUtil.TranslateDeepLAsync(config.deepl.url, InputTxt, LanguageEnum.EN, LanguageEnum.AUTO); - var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, Text, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); + var translateResp = await TranslateUtil.TranslateBaiduAsync(config.baidu.appid, config.baidu.secretKey, InputTxt, LanguageEnumDict[OutputComboSelected], LanguageEnumDict[InputComboSelected]); - if (translateResp == string.Empty) - { - OutputTxt = "翻译出错,请稍候再试..."; - return; - } - OutputTxt = translateResp; - } - catch (Exception ex) + if (translateResp == string.Empty) { - OutputTxt = ex.Message; + OutputTxt = "翻译出错,请稍候再试..."; + return; } - }); + OutputTxt = translateResp; + } + catch (Exception ex) + { + OutputTxt = ex.Message; + } } public ICommand TranslateCmd { get; private set; }