2014-03-27 70 views
0

對於我的android應用程序,是否可以使用Deployment.Current.Dispatcher.BeginInvoke進行快速而又髒的替換?它爲我的Windows手機應用程序華麗的工作,但我使用Xamarin嘗試複製我的應用程序的Android,我無法找出解決方案。Android的等效於Windows Phone的Deployment.Current.Dispatcher.BeginInvoke?

這裏就是正在使用它的代碼:

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的替代工作,但斷碼的關鍵部分是部署.current.dispatcher位。

如果沒有一個好的方法來做到這一點,你能幫助我更好地理解這條線是如何工作的,這樣我就可以嘗試解決問題了嗎?

編輯: 我張貼了這個問題到reddit,並針對這一點:http://developer.android.com/reference/android/os/AsyncTask.html,這似乎是我所需要的,只是需要我做一些重新組織

回答

0

如果您正在使用Deployment.Current .Dispatcher.BeginInvoke在UI線程上運行代碼,請嘗試Activity#runOnUiThread(Runnable)

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

活動需要調用它。它看起來像你的代碼已經是裏面一個,這樣取代:

Deployment.Current.Dispatcher.BeginInvoke 

用:你現在已經從另一個線程創建活動的引用,因此垃圾收集可能會非常棘手

this.RunOnUiThread 

注意。異步等待可能是更好的選擇,但即使如此,仍有GC問題需要尋找。最好的選擇是創建一個可觀察的模式,而不是直接調用UI線程。

0

的Visual Studio 7.3

在最新Xamarin

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