繁体   English   中英

Android是否等效于Windows Phone的Deployment.Current.Dispatcher.BeginInvoke?

[英]Android equivalent to Deployment.Current.Dispatcher.BeginInvoke for windows phone?

是否可以为Android应用程序使用Deployment.Current.Dispatcher.BeginInvoke的快捷替代品? 它对于我的Windows Phone应用程序非常有效,但是我正在使用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替换,因此有很多注释的代码,但是损坏的代码的关键部分是deploy.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线程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM