2013-04-16 28 views
0

我的應用程序需要用戶名和密碼,就無比的hyperlinkbutton,這些值被髮送到服務器,因此服務器返回類似PASS:客戶端ID。我希望只有在responseString包含PASS的情況下才能導航到SecondPage.xaml(來自MainPage.xaml.cs)。 這裏是我的代碼:的NullReferenceException另一個XAML

namespace aquila1 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     static string username; 
     static string password; 
     static string rs; 

     static NavigationService ns = new NavigationService(); 

     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 


     } 

     private static ManualResetEvent allDone = new ManualResetEvent(true); 

     private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e) 
     { 
      username = textbox1.Text; 
      password = textbox2.Text; 
      System.Diagnostics.Debug.WriteLine(username); 
      System.Diagnostics.Debug.WriteLine(password); 

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://60.243.245.181/fms_tracking/php/mobile_login.php?username=" + username + "&password=" + password); 


      request.ContentType = "application/x-www-form-urlencoded"; 

      // Set the Method property to 'POST' to post data to the URI. 
      request.Method = "POST"; 

      // start the asynchronous operation 
      request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); 

      // Keep the main thread from continuing while the asynchronous 
      // operation completes. A real world application 
      // could do something useful such as updating its user interface. 
      allDone.WaitOne(); 



     } 


     private static void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
     { 

      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

      // End the operation 
      Stream postStream = request.EndGetRequestStream(asynchronousResult); 

      // Console.WriteLine("Please enter the input data to be posted:"); 
      string postData = username + "+" + password; 
      System.Diagnostics.Debug.WriteLine(postData); 
      // Convert the string into a byte array. 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

      // Write to the request stream. 
      postStream.Write(byteArray, 0, postData.Length); 
      postStream.Close(); 

      // Start the asynchronous operation to get the response 
      request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
     } 

     private static void GetResponseCallback(IAsyncResult asynchronousResult) 
     { 
      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

      // End the operation 
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
      Stream streamResponse = response.GetResponseStream(); 
      StreamReader streamRead = new StreamReader(streamResponse); 
      string responseString = streamRead.ReadToEnd(); 
      rs = responseString; 
      System.Diagnostics.Debug.WriteLine(responseString); 
      System.Diagnostics.Debug.WriteLine("@@@@@"); 
      System.Diagnostics.Debug.WriteLine(rs); 

      // Close the stream object 
      streamResponse.Close(); 
      streamRead.Close(); 

      // Release the HttpWebResponse 
      response.Close(); 
      move2(); 


      allDone.Set(); 

     } 


     private static void move2() 
     { 

      string[] rs1 = rs.Split(':'); 
      if ((rs1[0].Trim()).Equals("PASS")) 
      { 

       ns.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative)); 
      } 
      else 
      { 
       MessageBox.Show(rs); 
      } 
     } 



    } 
} 

在運行的代碼,我總是得到的NullReferenceException。

Plz幫我找到錯誤並提出更正。

在此先感謝

+4

哪一行引發異常? –

+0

@EsotericScreenName:ns.Navigate(new Uri(「/ SecondPage.xaml」,UriKind.Relative));這行引發異常 – user2090226

回答

0

你最有可能得到錯誤,因爲的NavigationService找不到資源/SecondPage.xaml。 SecondPage是否位於項目的根目錄?

這也可以通過努力目標資源被加載之前進行導航(例如,通過頁面的構造函數中進行導航)引起的,但沒有立即出現是你的問題。

This answer表明,改變命名空間或程序集名稱後出現此問題。它指出,清理項目,確保所有bin和obj文件夾都是空的,然後重新編譯將會修復它。但是,它的參考鏈接已經死亡。

相關問題