0

我正在开发连接到服务并使用一些 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));
        }
4

2 回答 2

0

解决了!

在这种情况下,我使用了 try and catch 。完美运行:)

于 2012-06-20T10:57:43.473 回答
0

如果您的意思是要捕获 Web 客户端调用中发生的异常,那么它应该ErrorDownloadStringCompletedEventArgs.

void w_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if(e.Error != null)
    {
        MessageBox.Show("An error occurred!");
    }
    else
    {
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }
}
于 2012-06-19T10:18:55.113 回答