主要是用 async和 await 调用 RunOnUiThread来更新。
调用函数:
//异步加载数据开始 doInBackground (); //异步加载数据开始end protected async void doInBackground () { var result = await Task.Factory.StartNew((Func<string>)onSuccessfulLogin); if (result=="") { await Task.Factory.StartNew (() => { RunOnUiThread (delegate() { //跳转到主页面 Intent intent = new Intent(); intent.SetClass(this, typeof(Main)); StartActivity(intent); Finish (); }); }); } } protected string onSuccessfulLogin () { try { Thread.Sleep (2000); Hashtable pars = new Hashtable(); pars.Add("UserName",mAccount); pars.Add("Password", mPassword); UserInfoDao userinfodao = new UserInfoDao (); userinfomodel = userinfodao.getWebservice (pars); //判断登录 if (userinfomodel != null) { if (userinfomodel.UserID > 0) { //保持登录的状态 bgein ISharedPreferences shared = GetSharedPreferences("UserInfo", FileCreationMode.Private); string value = shared.GetString("UserInfo", ""); value =userinfomodel.UserID.ToString().Trim()+"|"+userinfomodel.UserName+"|"+userinfomodel.Token ; ISharedPreferencesEditor editor = shared.Edit(); editor.PutString("UserInfo",value); editor.Commit(); //保持登录的状态 end } } else { return "用户名或密码错误!"; } } catch (Exception e) { return e.ToString(); } return ""; }
Mono 异步加载数据更新主线程,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/phinex/p/3843973.html