Xamarin.Android 跳转到主线程代码

发一段Xamarin for Android 的跳转到主线程代码:
Intent intent = new Intent();
intent.SetClass(this, typeof(Main));
StartActivity(intent);
Finish ();

这段代码截取自mono开发者联盟的文章:Xamarin.Android开发——异步加载数据更新主线程里的一段代码,所有代码如下:
//异步加载数据开始
doInBackground ();
//异步加载数据开始end
protected async void doInBackground ()
{
var result = await Task.Factory.StartNew((Func)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 “”;
}

未经允许不得转载:前端撸码笔记 » Xamarin.Android 跳转到主线程代码

上一篇:

下一篇: