尝试捕获并没有显示与AWAIT消息对话框 [英] Try-Catch doesn't show Message Dialog box with await
本文介绍了尝试捕获并没有显示与AWAIT消息对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码成功地从网络服务中获取数据,并将其显示在Windows 8桌面应用程序。
HttpClient的客户端=新的HttpClient();
HttpResponseMessage响应=等待client.GetAsync(HTTP://本地主机:12345 / API /项目);
VAR信息=新的List< SampleDataGroup>();
如果(response.IsSuccessStatusCode)
{
VAR内容=等待response.Content.ReadAsStringAsync();
VAR项目= JsonConvert.DeserializeObject<动态>(内容);
的foreach(在项目VAR数据)
{
变种infoSect =新SampleDataGroup
(
(串)data.Id.ToString(),
(串)data.Name,
(串),
(串)data.PhotoUrl,
(串)data.Description
);
info.Add(infoSect);
}
}
,否则
{
MessageDialog DLG =新MessageDialog(错误);
等待dlg.ShowAsync();
}
这确实不过需要通过互联网连接到工作。应用程序崩溃每次有没有互联网连接的时间。
我怎样写这个代码尝试捕捉
语句显示一个弹出消息,如果网络连接不工作
编辑:
我试图用 MessageDialog
的下方,这将停止应用程序崩溃,但它并不带来了该消息。
赶上(异常前)
{
MessageDialog ERR =新MessageDialog(ex.Message);
}
解决方案
别T优需要
字符串的errorMessage =的String.Empty;
试
{
HttpClient的客户端=新的HttpClient();
HttpResponseMessage响应=等待
client.GetAsync(HTTP://本地主机:12345 / API /项目);
VAR信息=新的List< SampleDataGroup>();
如果(response.IsSuccessStatusCode)
{
VAR内容=等待response.Content.ReadAsStringAsync();
VAR项目= JsonConvert.DeserializeObject<动态>(内容);
的foreach(在项目VAR数据)
{
变种infoSect =新SampleDataGroup
(
(串)data.Id.ToString(),
(串)data.Name,
(串),
(串)data.PhotoUrl,
(串)data.Description
);
info.Add(infoSect);
}
}
,否则
{
的errorMessage =错误;
}
}
赶上(异常前)
{
的ErrorMessage = ex.Message;
}
如果(的errorMessage =的String.Empty!)
{
MessageDialog DLG =新MessageDialog(的errorMessage);
等待dlg.ShowAsync();
}
The following code successfully gets data from a web service and displays it in a Windows 8 desktop app.
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://localhost:12345/api/items");
var info = new List<SampleDataGroup>();
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var item = JsonConvert.DeserializeObject<dynamic>(content);
foreach (var data in item)
{
var infoSect = new SampleDataGroup
(
(string)data.Id.ToString(),
(string)data.Name,
(string)"",
(string)data.PhotoUrl,
(string)data.Description
);
info.Add(infoSect);
}
}
else
{
MessageDialog dlg = new MessageDialog("Error");
await dlg.ShowAsync();
}
This does however require an internet connection to work. The app crashes every time there is no internet connection.
How do I write a try catch
statement in this code to display a pop up message if the network connection doesnt work?
EDIT:
I am trying to use MessageDialog
below, and this stops the app from crashing, but it doesn't bring up the message.
catch (Exception ex)
{
MessageDialog err = new MessageDialog(ex.Message);
}
解决方案
Don't you need
string errorMessage = string.Empty;
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await
client.GetAsync("http://localhost:12345/api/items");
var info = new List<SampleDataGroup>();
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var item = JsonConvert.DeserializeObject<dynamic>(content);
foreach (var data in item)
{
var infoSect = new SampleDataGroup
(
(string)data.Id.ToString(),
(string)data.Name,
(string)"",
(string)data.PhotoUrl,
(string)data.Description
);
info.Add(infoSect);
}
}
else
{
errorMessage = "Error";
}
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
if (errorMessage != string.Empty)
{
MessageDialog dlg = new MessageDialog(errorMessage);
await dlg.ShowAsync();
}
?
这篇关于尝试捕获并没有显示与AWAIT消息对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文