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

时间:2014-03-27 16:03:14

标签: c# android xamarin

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

以下是使用它的代码:

TextView txtUSN = FindViewById<TextView> (Resource.Id.txtUserName);
TextView txtpwd = FindViewById<TextView> (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,这似乎是我需要的,只是要我做一些重组

3 个答案:

答案 0 :(得分:1)

Visual Studio 7.3

最新的Xamarin

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

Deployment.Current.Dispatcher.BeginInvoke

使用:

this.RunOnUiThread

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