123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using System;
- using System.Diagnostics;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using Microsoft.Win32;
- using TranslateTool.Model;
- using TranslateTool.Utilities;
- using TranslateTool.ViewModel;
- using TranslateTool.Views;
- namespace TranslateTool
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- private MainPageViewModel _viewModel;
-
- public MainWindow()
- {
- InitializeComponent();
- try
- {
- _viewModel = new MainPageViewModel();
- DataContext = _viewModel;
- _viewModel.LanguageItemsRefresh += OnLanguageItemsRefresh;
- _viewModel.HandleRepeatItem += OnHandleRepeatItem;
- }
- catch(Exception ex)
- {
- WriteErrorHelper.WriteErrorMessage($"MainWindow error:{ex}");
- }
-
- }
- private void OnHandleRepeatItem(object sender, HandleRepeatItemsArgs e)
- {
- var window = new HandleRepeatItemWindow
- {
- Owner = this,
- DataContext = new HandleRepaetItemPageViewModel(e.LanguageName,e.RepeatItems)
- };
- window.ShowDialog();
- }
- private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(obj, i);
- if (child != null && child is T)
- {
- return (T)child;
- }
- T childOfChild = FindVisualChild<T>(child);
- if (childOfChild != null)
- {
- return childOfChild;
- }
- }
- return null;
- }
- private void OnLanguageItemsRefresh(object sender, EventArgs e)
- {
- var sv = FindVisualChild<ScrollViewer>(KeyList);
- sv?.ScrollToTop();
- }
- private void ExportFile_OnClick(object sender, RoutedEventArgs e)
- {
- var saveFileDialog = new SaveFileDialog
- {
- Title = "选择语言文件",
- Filter = "txt文件|*.txt|json文件|*.json|所有文件|*.*",
- };
- if (saveFileDialog.ShowDialog() != true)
- {
- return;
- }
- var fileName = saveFileDialog.FileName;
- if (_viewModel != null)
- {
- _viewModel.ExportFileCommnad.Execute(fileName);
- }
- }
- private void ImportFile_OnClick(object sender, RoutedEventArgs e)
- {
- var openFileDialog = new OpenFileDialog
- {
- Title = "选择语言文件",
- Filter = "txt文件|*.txt|所有文件|*.*",
- Multiselect = true
- };
- if (openFileDialog.ShowDialog() != true)
- {
- return;
- }
- var fileName = openFileDialog.FileName;
- if (_viewModel != null)
- {
- _viewModel.ImportFileCommnad.Execute(fileName);
- }
- }
- private void ConvertOldFile_OnClick(object sender, RoutedEventArgs e)
- {
- var openFileDialog = new OpenFileDialog
- {
- Title = "选择语言文件",
- Filter = "ini文件|*.ini|json文件|*.json|所有文件|*.*"
- };
- if (openFileDialog.ShowDialog() != true)
- {
- return;
- }
- var fileName = openFileDialog.FileName;
- if (_viewModel != null)
- {
- _viewModel.ConvertFileCommnad.Execute(fileName);
- }
- }
- private void ImportLatestFile_OnClick(object sender, RoutedEventArgs e)
- {
- var openFileDialog = new OpenFileDialog
- {
- Title = "选择语言文件",
- Filter = "json文件|*.json|所有文件|*.*",
- };
- if (openFileDialog.ShowDialog() != true)
- {
- return;
- }
- var fileName = openFileDialog.FileName;
- if (_viewModel != null)
- {
- _viewModel.ImportNewFileCommnad.Execute(fileName);
- }
- }
- private void ClientComComboBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (_viewModel != null)
- {
- var window = new AddClientWindow
- {
- ResizeMode = ResizeMode.NoResize,
- Owner = this,
- DataContext = new AddClientPageViewModel(_viewModel),
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- window.ShowDialog();
- }
- }
- private void LangageComComboBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (_viewModel != null)
- {
- var window = new AddLanguageWindow
- {
- ResizeMode = ResizeMode.NoResize,
- Owner = this,
- DataContext = new AddLanguagePageViewModel(_viewModel),
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- window.ShowDialog();
- }
- }
- private void TextBox_GotFocus(object sender, RoutedEventArgs e)
- {
- var item = KeyList.SelectedItem;
- KeyList.ScrollIntoView(item);
- }
- private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key != Key.Enter) return;
- if (_viewModel != null)
- {
- var searchValue = SearchTxt.Text;
- _viewModel.SearchCommand.Execute(searchValue);
- }
- }
- private void Window_Closed(object sender, EventArgs e)
- {
- if (_viewModel != null)
- {
- _viewModel.SaveSelectedItem();
- }
- }
- /// <summary>
- /// export excel file
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ExportExcelFile_Click(object sender, RoutedEventArgs e)
- {
- var saveFileDialog = new SaveFileDialog
- {
- Title = "导出Excel文件",
- Filter = "Excel文件|*.xlsx|json文件|*.json|所有文件|*.*",
- };
- if (saveFileDialog.ShowDialog() != true)
- {
- return;
- }
- var fileName = saveFileDialog.FileName;
- if (_viewModel != null)
- {
- _viewModel.ExportExcelFileCommnad.Execute(fileName);
- }
- }
- /// <summary>
- /// export excel file
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ImportExcelFile_Click(object sender, RoutedEventArgs e)
- {
- var openFileDialog = new OpenFileDialog
- {
- Title = "选择语言文件",
- Filter = "Excel文件|*.xlsx|所有文件|*.*",
- Multiselect = true
- };
- if (openFileDialog.ShowDialog() != true)
- {
- return;
- }
- var fileName = openFileDialog.FileName;
- if (_viewModel != null)
- {
- _viewModel.ImportExcelFileCommnad.Execute(fileName);
- }
- }
- private void SelectEnglishFile_Click(object sender, RoutedEventArgs e)
- {
- }
- private void SelectFile_Click(object sender, RoutedEventArgs e)
- {
- }
- }
- }
|