我正在開發連接到服務並使用一些JSON數據的應用程序。消費很好(JSON.net的岩石),但我想知道我應該抓住異常錯誤和顯示簡單的MessageBox?嘗試在幾個地方,但仍然我的應用程序正在關閉。或者,也許我應該根據包含錯誤標籤的json響應來做到這一點?我認爲正常的錯誤處理可能會更容易,但在現在腦子裏一片空白點..WebClient方法在哪裏捕獲異常?
代碼如下:
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));
}
我試過這一個。但錯誤再次出現。它出現在UserSettings對象無法填充的時刻。情景是:如果登錄數據沒問題,我得到對象調用UserSettings,如果沒有,我只有一個名爲error的json條目:「error」 – dargod
所以我想我應該嘗試捕獲異常,當我知道我會得到什麼結果得到 – dargod