在 xamarin 中处理异常
Handling exceptions in xamarin
我在尝试以最佳方式处理一些错误时遇到了一些困难。例如,我的一个案例是 NullReferenceException。
为了更清楚,让我用几句话解释一下。当我调用服务器接收一些信息时,在某些情况下服务器可能会出现一些问题,它会 return 当然 null
.
我所做的是显示一个警告,让用户知道他可以稍后再试。在此之后,我尝试在上一页中发送给他。毕竟我的应用程序仍然崩溃。
我想做的是简单地显示警报,然后让用户停留在同一页面而不会破坏应用程序。
这些是我的几段代码:
tasks.cs
public async Task<List<Idea>> GetIdeaAsync(string accesToken)
{
List<Idea> ideas = null;
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesToken);
var json = await client.GetStringAsync("http://www.getdata.de/api/ideas/");
var ideas = JsonConvert.DeserializeObject<List<Idea>>(json);
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Server Error", "There has been an server error. Please try later.", "OK");
if (ideas == null)
{
await Application.Current.MainPage.Navigation.PopAsync(); //actually I would like to stay in the same page
}
}
return ideas;
}
view.xaml.cs
private async void Button_Clicked(object sender, EventArgs e)
{
Tasks ts = new Tasks();
var ideas = await ts.GetIdeasAsync();
if (ideas == null)
{
Debug.WriteLine("hello");
//do nothing since the display alert is already shown
}
else
{
//code here
}
如果有人能指导我采用 "best-practice" 方法,我将不胜感激。谢谢:)
您在 try 块中声明 ideas
,然后试图在 catch
块中访问它,但它超出了范围。 (Visual Studio 应该给出智能感知错误)
另外,无论何时操作 UI,你都应该在主线程上进行。所以将您的 DisplayAlert()
代码移动到
Device.BeginInvokeOnMainThread(async () =>
{
// await DisplayAlert(); move it into here
});
此外,任何 PopAsync
或 PushAsync
调用也应在主 UI 线程上完成。但是在异步调用 API 之后调用 PopAsync
并不是一个好主意,因为在调用 returns.
时用户可能已经按下了后退按钮。
至于 NullReferenceException,在将其传递给 DeserializeObject()
函数之前检查 json
是否为 null。
这个问题其实很明显,因为我在捕获异常之后会继续编写代码。所以我所做的是:
public async Task<List<Idea>> GetIdeaAsync(string accesToken)
{
List<Idea> ideas = null;
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesToken);
var json = await client.GetStringAsync("http://www.getdata.de/api/ideas/");
var ideas = JsonConvert.DeserializeObject<List<Idea>>(json);
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Server Error", "There has been an server error. Please try later.", "OK");
if (ideas == null)
{
//actually I would like to stay in the same page
return null; //-- added this line
}
}
return ideas;
}
也许这不是最好的主意,但它对我有用。任何其他方法将不胜感激。 :)
我在尝试以最佳方式处理一些错误时遇到了一些困难。例如,我的一个案例是 NullReferenceException。
为了更清楚,让我用几句话解释一下。当我调用服务器接收一些信息时,在某些情况下服务器可能会出现一些问题,它会 return 当然 null
.
我所做的是显示一个警告,让用户知道他可以稍后再试。在此之后,我尝试在上一页中发送给他。毕竟我的应用程序仍然崩溃。
我想做的是简单地显示警报,然后让用户停留在同一页面而不会破坏应用程序。
这些是我的几段代码:
tasks.cs
public async Task<List<Idea>> GetIdeaAsync(string accesToken)
{
List<Idea> ideas = null;
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesToken);
var json = await client.GetStringAsync("http://www.getdata.de/api/ideas/");
var ideas = JsonConvert.DeserializeObject<List<Idea>>(json);
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Server Error", "There has been an server error. Please try later.", "OK");
if (ideas == null)
{
await Application.Current.MainPage.Navigation.PopAsync(); //actually I would like to stay in the same page
}
}
return ideas;
}
view.xaml.cs
private async void Button_Clicked(object sender, EventArgs e)
{
Tasks ts = new Tasks();
var ideas = await ts.GetIdeasAsync();
if (ideas == null)
{
Debug.WriteLine("hello");
//do nothing since the display alert is already shown
}
else
{
//code here
}
如果有人能指导我采用 "best-practice" 方法,我将不胜感激。谢谢:)
您在 try 块中声明 ideas
,然后试图在 catch
块中访问它,但它超出了范围。 (Visual Studio 应该给出智能感知错误)
另外,无论何时操作 UI,你都应该在主线程上进行。所以将您的 DisplayAlert()
代码移动到
Device.BeginInvokeOnMainThread(async () =>
{
// await DisplayAlert(); move it into here
});
此外,任何 PopAsync
或 PushAsync
调用也应在主 UI 线程上完成。但是在异步调用 API 之后调用 PopAsync
并不是一个好主意,因为在调用 returns.
至于 NullReferenceException,在将其传递给 DeserializeObject()
函数之前检查 json
是否为 null。
这个问题其实很明显,因为我在捕获异常之后会继续编写代码。所以我所做的是:
public async Task<List<Idea>> GetIdeaAsync(string accesToken)
{
List<Idea> ideas = null;
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesToken);
var json = await client.GetStringAsync("http://www.getdata.de/api/ideas/");
var ideas = JsonConvert.DeserializeObject<List<Idea>>(json);
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Server Error", "There has been an server error. Please try later.", "OK");
if (ideas == null)
{
//actually I would like to stay in the same page
return null; //-- added this line
}
}
return ideas;
}
也许这不是最好的主意,但它对我有用。任何其他方法将不胜感激。 :)