Android相当于Windows Phone的Deployment.Current.Dispatcher.BeginInvoke?

我可以用于Android应用程序的Deployment.Current.Dispatcher.BeginInvoke有一个快速和脏的替代品吗? 它为我的Windows手机应用程序工作华丽,但我正在使用xamarin尝试复制我的Android应用程序,我无法找到解决方案。

这是使用它的代码:

TextView txtUSN = FindViewById (Resource.Id.txtUserName); TextView txtpwd = FindViewById (Resource.Id.txtPassword); string usn = txtUSN.Text; string pwd = txtpwd.Text; string requestToken = "http://192.168.0.10/cschome/webdb1.aspx?cmd=login&usn=" + user + "&pwd=" + pass; //var request = (HttpWebRequest)WebRequest.Create(new Uri(requestToken)); var request = (HttpWebRequest)WebRequest.Create (new Uri (requestToken)); request.BeginGetResponse (r => { var httpRequest = (HttpWebRequest)r.AsyncState; var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r); using (var reader = new StreamReader(httpResponse.GetResponseStream())) { var response = reader.ReadToEnd(); Deployment.Current.Dispatcher.BeginInvoke(new Action(() => { string[] tempArray = response.Split('|'); if (tempArray[2].Substring(0, 2) == "OK") //check to make sure the login was complete { if (tempArray[2].Contains("1"))//If the user is level one, dol this { //NavigationService.Navigate(new Uri("/EntryView.xaml?token=" + tempArray[1] + "&user=" + tempArray[3] + "&email=" + tempArray[4], UriKind.Relative)); } else { if (tempArray[2].Contains("2")) //if the user is a level 2 user, do this { } else { //MessageBox.Show("Error: Invalid Security Token"); //if the logon was a success, but the security token lacks a level number } } } else //logon failure caviot { if (response.Contains("NOK usn")) { //MessageBox.Show("A logon error occured. Please check your username and password and try again"); } if (response.Contains("NOK pwd")) { //MessageBox.Show("Password Missmatch: Please check the spelling and capitolization"); } if (response.Contains("NOK locked")) { //MessageBox.Show("The user account is locked. Please contact your helpdesk"); } } })); } }, request); } 

请注意,有很多注释代码,因为我正在进行android替换,但是破解代码的关键部分是deployment.current.dispatcher位。

如果没有一个很好的方法来做到这一点,你能帮助我更好地理解这条线是如何工作的,这样我就可以尝试解决这个问题吗?

编辑:我把这个问题发布到reddit,并被引导到这个: http : //developer.android.com/reference/android/os/AsyncTask.html ,这似乎是我需要的,只需要我做一些重新-组织

Visual Studio 7.3

在最新的Xamarin中

 Device.BeginInvokeOnMainThread(() => { button.IsVisible = true; }); 

如果您使用Deployment.Current.Dispatcher.BeginInvoke在UI线程上运行代码,请尝试Activity#runOnUiThread(Runnable) 。

 runOnUiThread(new Runnable() { @Override public void run() { // Write your code here. } }); 

活动需要调用它。 看起来您的代码已在其中,因此替换:

 Deployment.Current.Dispatcher.BeginInvoke 

有:

 this.RunOnUiThread 

请注意,您现在已经从另一个线程创建了对Activity的引用,因此垃圾收集可能很棘手。 异步等待可能是更好的选择,但即便如此,仍有GC问题需要寻找。 最好的选择是创建一个可观察的模式,而不是直接调用UI线程。