fix: catch not connect to network exception

master
ZGGSONG 2 years ago
parent d94b20f39b
commit 866d896df8

@ -60,6 +60,8 @@ namespace STranslate.Utils
public static async Task<string> GetAsync(string urlpath) public static async Task<string> GetAsync(string urlpath)
{ {
using (var client = new HttpClient()) using (var client = new HttpClient())
{
try
{ {
var respContent = await client.GetAsync(urlpath); var respContent = await client.GetAsync(urlpath);
@ -67,6 +69,11 @@ namespace STranslate.Utils
return respStr; return respStr;
} }
catch (Exception ex)
{
throw ex;
}
}
} }
} }
} }

@ -60,6 +60,8 @@ namespace STranslate.Utils
/// <param name="source">当前语言</param> /// <param name="source">当前语言</param>
/// <returns></returns> /// <returns></returns>
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO) public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO)
{
try
{ {
Random rd = new Random(); Random rd = new Random();
string salt = rd.Next(100000).ToString(); string salt = rd.Next(100000).ToString();
@ -93,6 +95,11 @@ namespace STranslate.Utils
} }
return string.Empty; return string.Empty;
} }
catch (Exception ex)
{
throw ex;
}
}
// 计算MD5值 // 计算MD5值
public static string EncryptString(string str) public static string EncryptString(string str)
{ {

@ -18,8 +18,6 @@ namespace STranslate.ViewModel
private string Text; private string Text;
public MainVM() public MainVM()
{
try
{ {
//初始化界面参数 //初始化界面参数
InputCombo = LanguageEnumDict.Keys.ToList(); InputCombo = LanguageEnumDict.Keys.ToList();
@ -44,6 +42,8 @@ namespace STranslate.ViewModel
{ {
return string.IsNullOrEmpty(InputTxt) ? false : true; return string.IsNullOrEmpty(InputTxt) ? false : true;
}, async (_) => }, async (_) =>
{
try
{ {
Text = InputTxt; Text = InputTxt;
@ -63,14 +63,12 @@ namespace STranslate.ViewModel
return; return;
} }
OutputTxt = translateResp; OutputTxt = translateResp;
Clipboard.SetText(OutputTxt);
});
} }
catch (Exception ex) catch (Exception ex)
{ {
OutputTxt = ex.Message; OutputTxt = ex.Message;
} }
});
} }
public ICommand TranslateCmd { get; private set; } public ICommand TranslateCmd { get; private set; }