MainWindow.xaml.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using Microsoft.Win32;
  8. using TranslateTool.Model;
  9. using TranslateTool.Utilities;
  10. using TranslateTool.ViewModel;
  11. using TranslateTool.Views;
  12. namespace TranslateTool
  13. {
  14. /// <summary>
  15. /// MainWindow.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class MainWindow : Window
  18. {
  19. private MainPageViewModel _viewModel;
  20. public MainWindow()
  21. {
  22. InitializeComponent();
  23. try
  24. {
  25. _viewModel = new MainPageViewModel();
  26. DataContext = _viewModel;
  27. _viewModel.LanguageItemsRefresh += OnLanguageItemsRefresh;
  28. _viewModel.HandleRepeatItem += OnHandleRepeatItem;
  29. }
  30. catch(Exception ex)
  31. {
  32. WriteErrorHelper.WriteErrorMessage($"MainWindow error:{ex}");
  33. }
  34. }
  35. private void OnHandleRepeatItem(object sender, HandleRepeatItemsArgs e)
  36. {
  37. var window = new HandleRepeatItemWindow
  38. {
  39. Owner = this,
  40. DataContext = new HandleRepaetItemPageViewModel(e.LanguageName,e.RepeatItems)
  41. };
  42. window.ShowDialog();
  43. }
  44. private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
  45. {
  46. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  47. {
  48. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  49. if (child != null && child is T)
  50. {
  51. return (T)child;
  52. }
  53. T childOfChild = FindVisualChild<T>(child);
  54. if (childOfChild != null)
  55. {
  56. return childOfChild;
  57. }
  58. }
  59. return null;
  60. }
  61. private void OnLanguageItemsRefresh(object sender, EventArgs e)
  62. {
  63. var sv = FindVisualChild<ScrollViewer>(KeyList);
  64. sv?.ScrollToTop();
  65. }
  66. private void ExportFile_OnClick(object sender, RoutedEventArgs e)
  67. {
  68. var saveFileDialog = new SaveFileDialog
  69. {
  70. Title = "选择语言文件",
  71. Filter = "txt文件|*.txt|json文件|*.json|所有文件|*.*",
  72. };
  73. if (saveFileDialog.ShowDialog() != true)
  74. {
  75. return;
  76. }
  77. var fileName = saveFileDialog.FileName;
  78. if (_viewModel != null)
  79. {
  80. _viewModel.ExportFileCommnad.Execute(fileName);
  81. }
  82. }
  83. private void ImportFile_OnClick(object sender, RoutedEventArgs e)
  84. {
  85. var openFileDialog = new OpenFileDialog
  86. {
  87. Title = "选择语言文件",
  88. Filter = "txt文件|*.txt|所有文件|*.*",
  89. Multiselect = true
  90. };
  91. if (openFileDialog.ShowDialog() != true)
  92. {
  93. return;
  94. }
  95. var fileName = openFileDialog.FileName;
  96. if (_viewModel != null)
  97. {
  98. _viewModel.ImportFileCommnad.Execute(fileName);
  99. }
  100. }
  101. private void ConvertOldFile_OnClick(object sender, RoutedEventArgs e)
  102. {
  103. var openFileDialog = new OpenFileDialog
  104. {
  105. Title = "选择语言文件",
  106. Filter = "ini文件|*.ini|json文件|*.json|所有文件|*.*"
  107. };
  108. if (openFileDialog.ShowDialog() != true)
  109. {
  110. return;
  111. }
  112. var fileName = openFileDialog.FileName;
  113. if (_viewModel != null)
  114. {
  115. _viewModel.ConvertFileCommnad.Execute(fileName);
  116. }
  117. }
  118. private void ImportLatestFile_OnClick(object sender, RoutedEventArgs e)
  119. {
  120. var openFileDialog = new OpenFileDialog
  121. {
  122. Title = "选择语言文件",
  123. Filter = "json文件|*.json|所有文件|*.*",
  124. };
  125. if (openFileDialog.ShowDialog() != true)
  126. {
  127. return;
  128. }
  129. var fileName = openFileDialog.FileName;
  130. if (_viewModel != null)
  131. {
  132. _viewModel.ImportNewFileCommnad.Execute(fileName);
  133. }
  134. }
  135. private void ClientComComboBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  136. {
  137. if (_viewModel != null)
  138. {
  139. var window = new AddClientWindow
  140. {
  141. ResizeMode = ResizeMode.NoResize,
  142. Owner = this,
  143. DataContext = new AddClientPageViewModel(_viewModel),
  144. WindowStartupLocation = WindowStartupLocation.CenterOwner
  145. };
  146. window.ShowDialog();
  147. }
  148. }
  149. private void LangageComComboBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  150. {
  151. if (_viewModel != null)
  152. {
  153. var window = new AddLanguageWindow
  154. {
  155. ResizeMode = ResizeMode.NoResize,
  156. Owner = this,
  157. DataContext = new AddLanguagePageViewModel(_viewModel),
  158. WindowStartupLocation = WindowStartupLocation.CenterOwner
  159. };
  160. window.ShowDialog();
  161. }
  162. }
  163. private void TextBox_GotFocus(object sender, RoutedEventArgs e)
  164. {
  165. var item = KeyList.SelectedItem;
  166. KeyList.ScrollIntoView(item);
  167. }
  168. private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
  169. {
  170. if (e.Key != Key.Enter) return;
  171. if (_viewModel != null)
  172. {
  173. var searchValue = SearchTxt.Text;
  174. _viewModel.SearchCommand.Execute(searchValue);
  175. }
  176. }
  177. private void Window_Closed(object sender, EventArgs e)
  178. {
  179. if (_viewModel != null)
  180. {
  181. _viewModel.SaveSelectedItem();
  182. }
  183. }
  184. /// <summary>
  185. /// export excel file
  186. /// </summary>
  187. /// <param name="sender"></param>
  188. /// <param name="e"></param>
  189. private void ExportExcelFile_Click(object sender, RoutedEventArgs e)
  190. {
  191. var saveFileDialog = new SaveFileDialog
  192. {
  193. Title = "导出Excel文件",
  194. Filter = "Excel文件|*.xlsx|json文件|*.json|所有文件|*.*",
  195. };
  196. if (saveFileDialog.ShowDialog() != true)
  197. {
  198. return;
  199. }
  200. var fileName = saveFileDialog.FileName;
  201. if (_viewModel != null)
  202. {
  203. _viewModel.ExportExcelFileCommnad.Execute(fileName);
  204. }
  205. }
  206. /// <summary>
  207. /// export excel file
  208. /// </summary>
  209. /// <param name="sender"></param>
  210. /// <param name="e"></param>
  211. private void ImportExcelFile_Click(object sender, RoutedEventArgs e)
  212. {
  213. var openFileDialog = new OpenFileDialog
  214. {
  215. Title = "选择语言文件",
  216. Filter = "Excel文件|*.xlsx|所有文件|*.*",
  217. Multiselect = true
  218. };
  219. if (openFileDialog.ShowDialog() != true)
  220. {
  221. return;
  222. }
  223. var fileName = openFileDialog.FileName;
  224. if (_viewModel != null)
  225. {
  226. _viewModel.ImportExcelFileCommnad.Execute(fileName);
  227. }
  228. }
  229. private void SelectEnglishFile_Click(object sender, RoutedEventArgs e)
  230. {
  231. }
  232. private void SelectFile_Click(object sender, RoutedEventArgs e)
  233. {
  234. }
  235. }
  236. }