public static void DoWork(this UIViewController controller, Action work, Action<Exception> error = null, Action final = null) { MBProgressHUD hud = null; hud = new MBProgressHUD(controller.View.Superview) {Mode = MBProgressHUDMode.Indeterminate, TitleText = "Loading..."}; controller.View.Superview.AddSubview(hud); hud.Show(true); ThreadPool.QueueUserWorkItem(delegate { try { Utilities.PushNetworkActive(); work(); } catch (Exception e) { if (error != null) error(e); controller.InvokeOnMainThread(delegate { Utilities.ShowAlert("Error", e.Message); }); } finally { Utilities.PopNetworkActive(); if (final != null) controller.InvokeOnMainThread(() => final()); } if (hud != null) { controller.InvokeOnMainThread(delegate { hud.Hide(true); hud.RemoveFromSuperview(); }); } }); }
/// <summary> /// Begins the login process. /// </summary> private void BeginLogin() { MBProgressHUD hud = null; string username = null, password = null; Account loggedInAccount = null; //The nice hud InvokeOnMainThread(delegate { username = User.Text; password = Password.Text; hud = new MBProgressHUD(View) {Mode = MBProgressHUDMode.Indeterminate, TitleText = "Logging In..."}; View.AddSubview(hud); hud.Show(true); }); try { loggedInAccount = Login (username, password); } catch (Exception e) { Console.WriteLine("Error = " + e.Message); } InvokeOnMainThread(delegate { //Dismiss the hud hud.Hide(true); hud.RemoveFromSuperview(); if (loggedInAccount == null) { Utilities.ShowAlert("Unable to Authenticate", "Unable to login as user " + username + ". Please check your credentials and try again."); return; } var account = Application.Accounts.Find(loggedInAccount.Username); //Account does not exist! Add it! if (account == null) { account = loggedInAccount; Application.Accounts.Insert(account); } //Account already exists. Update the password just incase it changed... else { account.Password = Password.Text; account.Update(); Application.SetUser(account); } if (LoginComplete != null) LoginComplete(account); }); }