1+
2+ //
3+ //
4+ //
5+ //
6+ // TranslateUI
7+ // by Luke Zhang (GitHub@zsr-lukezhang)
8+ // original idea by Lingbo (GitHub@lingbopro)
9+ // Application Version: 1.1
10+ //
11+ //
12+ //
13+
114using System ;
215using System . Net . Http ;
316using System . Text ;
720using Microsoft . UI . Xaml . Controls ;
821using System . Diagnostics ;
922using System . Runtime . InteropServices ;
23+ using Windows . ApplicationModel . DataTransfer ;
24+ using Microsoft . UI . Xaml . Media ;
1025
1126namespace TranslateUI
1227{
@@ -22,13 +37,28 @@ public MainWindow()
2237
2338 private async void TranslateButton_Click ( object sender , RoutedEventArgs e )
2439 {
40+ PB_Wait . ShowError = false ;
2541 PB_Wait . Visibility = Visibility . Visible ;
2642 TranslateButton . IsEnabled = false ;
2743
2844 string inputText = TB_Input . Text ;
2945 string selectedOutputLang = CB_OutputLang . SelectedItem ? . ToString ( ) ;
3046 string realOutputLang = GetLanguageCode ( selectedOutputLang ) ;
3147
48+ if ( inputText == "_Dev_Mode_" )
49+ {
50+ ShowError ( "Hello, world!" , "Emm...\n It seems like you are a software developer...\n Please join us to fix and add more features to this app!" ) ;
51+ // 使用 DispatcherQueue 在 UI 线程上更新控件
52+ _ = DispatcherQueue . TryEnqueue ( ( ) =>
53+ {
54+ TB_Output . Text = "Hello, world!" ;
55+ PB_Wait . Visibility = Visibility . Collapsed ;
56+ TranslateButton . IsEnabled = true ;
57+ } ) ;
58+
59+ return ;
60+ }
61+
3262 if ( ! string . IsNullOrEmpty ( realOutputLang ) )
3363 {
3464 try
@@ -42,12 +72,15 @@ private async void TranslateButton_Click(object sender, RoutedEventArgs e)
4272 TB_Output . Text = translatedText ;
4373 PB_Wait . Visibility = Visibility . Collapsed ;
4474 TranslateButton . IsEnabled = true ;
75+ PB_Wait . ShowError = true ;
4576 } ) ;
4677 }
4778 catch ( COMException comEx )
4879 {
4980 Debug . WriteLine ( $ "COMException: { comEx . Message } ") ;
5081 Debug . WriteLine ( $ "COMException StackTrace: { comEx . StackTrace } ") ;
82+ ShowError ( "COM Exception" , $ "Exception:\n { comEx . Message } \n Exception StackTrace:\n { comEx . StackTrace } ") ;
83+
5184 // 使用 DispatcherQueue 在 UI 线程上更新控件
5285 _ = DispatcherQueue . TryEnqueue ( ( ) =>
5386 {
@@ -59,6 +92,8 @@ private async void TranslateButton_Click(object sender, RoutedEventArgs e)
5992 {
6093 Debug . WriteLine ( $ "Exception: { ex . Message } ") ;
6194 Debug . WriteLine ( $ "Exception StackTrace: { ex . StackTrace } ") ;
95+ ShowError ( "Exception" , $ "Exception:\n { ex . Message } \n Exception StackTrace:\n { ex . StackTrace } ") ;
96+
6297 // 使用 DispatcherQueue 在 UI 线程上更新控件
6398 _ = DispatcherQueue . TryEnqueue ( ( ) =>
6499 {
@@ -70,6 +105,8 @@ private async void TranslateButton_Click(object sender, RoutedEventArgs e)
70105 else
71106 {
72107 Debug . WriteLine ( "Error: Invalid language selection." ) ;
108+ ShowError ( "Error" , "Invalid language selection." ) ;
109+
73110 // 使用 DispatcherQueue 在 UI 线程上更新控件
74111 _ = DispatcherQueue . TryEnqueue ( ( ) =>
75112 {
@@ -121,10 +158,12 @@ public string GetTranslatedText(string json)
121158 catch ( Exception ex )
122159 {
123160 Debug . WriteLine ( $ "Error parsing JSON: { ex . Message } ") ;
161+ ShowError ( "Error Parsing JSON" , ex . Message ) ;
124162 return string . Empty ;
125163 }
126164
127165 Debug . WriteLine ( "Error: Translation empty." ) ;
166+ ShowError ( "Error" , "Translation Empty" ) ;
128167 return string . Empty ;
129168 }
130169
@@ -145,5 +184,51 @@ private async Task<string> SendPostRequest(string translateToLang, string conten
145184 HttpResponseMessage response = await client . PostAsync ( $ "https://edge.microsoft.com/translate/translatetext?from=&to={ translateToLang } ", requestContent ) . ConfigureAwait ( false ) ;
146185 return await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
147186 }
187+
188+ private void ShowError ( string title , string content )
189+ {
190+ var dispatcherQueue = this . DispatcherQueue ;
191+ if ( dispatcherQueue != null )
192+ {
193+ _ = dispatcherQueue . TryEnqueue ( async ( ) =>
194+ {
195+ var dialog = new ContentDialog
196+ {
197+ Title = title ,
198+ Content = $ ":(\n You reached an error.\n You can try to solve this problem yourself,\n or post a new issue on GitHub.\n ===================================\n { content } ",
199+ XamlRoot = this . Content . XamlRoot , // 设置 XamlRoot
200+ Width = 400 , // 设置宽度
201+ Height = 300 , // 设置高度
202+ PrimaryButtonText = "OK" ,
203+ SecondaryButtonText = "Copy to Clipboard" ,
204+ DefaultButton = ContentDialogButton . Primary ,
205+ PrimaryButtonStyle = new Style ( typeof ( Button ) )
206+ {
207+ Setters =
208+ {
209+ new Setter ( Button . BackgroundProperty , new SolidColorBrush ( Microsoft . UI . Colors . Blue ) ) ,
210+ new Setter ( Button . ForegroundProperty , new SolidColorBrush ( Microsoft . UI . Colors . White ) )
211+ }
212+ }
213+ } ;
214+
215+ dialog . SecondaryButtonClick += ( sender , args ) =>
216+ {
217+ var dataPackage = new DataPackage ( ) ;
218+ dataPackage . SetText ( $ "Error title:\n { title } \n Error Message:\n { content } ") ;
219+ Clipboard . SetContent ( dataPackage ) ;
220+ } ;
221+
222+ await dialog . ShowAsync ( ) ;
223+ } ) ;
224+ }
225+ else
226+ {
227+ // 处理 DispatcherQueue 为 null 的情况
228+ // 我不知道怎么处理(
229+ // Luke Zhang
230+ // 2024/12/08
231+ }
232+ }
148233 }
149234}
0 commit comments