Android相当于windows phone的Deployment.Current.Dispatcher.BeginI
本文关键字:Current Dispatcher BeginI Deployment 相当于 windows phone Android | 更新日期: 2023-09-27 17:53:58
是否有一个快速和脏的替代Deployment.Current.Dispatcher.BeginInvoke,我可以使用我的android应用程序?它在我的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替换,但关键的破碎代码是deploy .current.dispatcher位。
如果没有一个好的方法来做到这一点,你能帮我更好地理解这一行是如何工作的,这样我就可以试着做一个工作?
编辑:我把这个问题贴在reddit上,并被指向这个:http://developer.android.com/reference/android/os/AsyncTask.html,这似乎是我需要的,只需要我做一些重组
Visual Studio 7.3
In Latest Xamarin
Device.BeginInvokeOnMainThread(() => {
button.IsVisible = true;
});
如果您正在使用Deployment.Current.Dispatcher.BeginInvoke在UI线程上运行代码,请尝试活动#runOnUiThread(Runnable)。
runOnUiThread(new Runnable() {
@Override
public void run() {
// Write your code here.
}
});
Activity需要调用它。看起来你的代码已经在其中了,所以替换:
Deployment.Current.Dispatcher.BeginInvoke
:
this.RunOnUiThread
请注意,您现在已经从另一个线程创建了对Activity的引用,因此垃圾收集可能会很棘手。异步等待可能是更好的选择,但即使这样也有GC问题需要寻找。最好的选择是创建一个可观察模式,而不是直接调用UI线程。