Skip to content

Commit ad7e775

Browse files
committed
Version 1.1!
1 parent e5facf6 commit ad7e775

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

TranslateUI/MainWindow.xaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@
117117
VerticalAlignment="Center"
118118
HorizontalAlignment="Center"
119119
Width="300"
120-
Height="50">
120+
Height="50"
121+
IsSpellCheckEnabled="False"
122+
AcceptsReturn="True">
121123
</TextBox>
122124
</StackPanel>
123125
<StackPanel Name="SP_TranslateButton"
@@ -164,7 +166,8 @@
164166
Width="300"
165167
Height="50"
166168
ScrollViewer.VerticalScrollBarVisibility="Auto"
167-
ScrollViewer.HorizontalScrollBarVisibility="Auto" >
169+
ScrollViewer.HorizontalScrollBarVisibility="Auto"
170+
AcceptsReturn="True">
168171
</TextBox>
169172
</StackPanel>
170173
</StackPanel>

TranslateUI/MainWindow.xaml.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
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+
114
using System;
215
using System.Net.Http;
316
using System.Text;
@@ -7,6 +20,8 @@
720
using Microsoft.UI.Xaml.Controls;
821
using System.Diagnostics;
922
using System.Runtime.InteropServices;
23+
using Windows.ApplicationModel.DataTransfer;
24+
using Microsoft.UI.Xaml.Media;
1025

1126
namespace 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...\nIt seems like you are a software developer...\nPlease 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}\nException 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}\nException 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 = $":(\nYou reached an error.\nYou can try to solve this problem yourself,\nor 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}\nError 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

Comments
 (0)