【发布时间】:2017-12-02 14:51:00
【问题描述】:
我正在使用 Xamarin.Forms 创建一个 android 应用程序。 在频繁地关闭和打开互联网连接时,我收到 System.Net.WebException ConnectFailure 异常。我试图在我的 PCL 代码中处理它,但它并没有被困在那里。 以下是 Xamarin.Forms 共享项目的示例代码。
public async Task GetNewSomething(CancellationToken token)
{
await Task.Run(async () =>
{
while (true)
{
token.ThrowIfCancellationRequested();
await Task.Delay(10000, token);
if (CrossConnectivity.Current.IsConnected) // check if internet is available
{
try
{
//Make server call to get data
FacilityManager.GetAllFacilities(list =>
{
//For testing purpose : Intentionally thowing an exception to check if we can catch it in the catch block below.
throw new WebException();
MessagingCenter.Send(list, "FreshFacilityListFromServer");
}, 0, true);
}
catch (WebException ex)
{
//It is never gets caught here. :(
}
}
}
}, token);
}
}
有人可以指导我如何在给定的 catch 块中处理 WebException。感谢所有反馈。
谢谢!
【问题讨论】:
标签: c# exception-handling xamarin.forms system.net.webexception