我正在开发连接到服务并使用一些 JSON 数据的应用程序。消费效果很好(JSON.net 摇滚),但我想知道我应该在哪里捕获异常错误并显示简单的 MessageBox?在几个地方尝试过,但我的应用程序仍然关闭。或者也许我应该根据包含错误标签的 json 响应来做?我认为正常的错误处理可能会更容易,但现在我脑子里有空白..
代码如下:
private void LoginLoginButton_Click(object sender, System.EventArgs e)
{
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false;
ProgressOverlay.Show();
GenerateLoginString();
var w = new SharpGIS.GZipWebClient();
Observable.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted")
.Subscribe(r =>
{
var settings = IsolatedStorageSettings.ApplicationSettings;
var deserializedRootObject = JsonConvert.DeserializeObject<RootObject>(r.EventArgs.Result);
UserSettings us = new UserSettings()
{
first_name = deserializedRootObject.user.first_name,
last_name = deserializedRootObject.user.last_name,
user_id = deserializedRootObject.user_id,
};
settings.Add("UserSettings", us);
settings.Save();
});
w.DownloadStringAsync(new Uri(UserUri));
w.DownloadStringCompleted += new DownloadStringCompletedEventHandler(w_DownloadStringCompleted);
}
void w_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}