2013-04-16 31 views
0

我的應用程序從mainpage.xaml收集用戶名和密碼,並向服務器發出httprequest。如果服務器的響應是PASS,那麼我想讓控件導航到另一個xaml頁面。我用下面的代碼在windows phone 7中,導航服務特定於一個頁面。我如何從外部頁面調用?

 if ((rs1[0].Trim()).Equals("PASS")) 
     { 
      NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative)); 
        } 
     else 
     { 
      MessageBox.Show(rs); 
     } 

其中rs1是一個字符串數組。

但我得到一個NullReferenceException。 Plz建議任何替代方式。提前致謝。

完整的代碼如下:

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); 
      } 
     } 



    } 
} 
+0

哪個對象是'null'? –

+0

@RowlandShaw:object不爲null,但是當控件進入下面的行時,NullReferenceException出現NavigationService.Navigate(new Uri(「/ SecondPage.xaml」,UriKind.Relative)); – user2090226

+0

@RowlandShaw:我也檢查了rs和rs1 [0]的內容。他們是根據需要。 – user2090226

回答

0

我的心理調試能力,告訴我你寫的代碼裏面之一: - 靜態構造函數 - 構造 - 調用OnNavigatedTo

一個名爲方法之前

一般而言,在輸入OnNavigatedTo方法之前,NavigationServicenull。 您可能想要從當前位置移動該邏輯。

無論如何它現在位於哪裏?

0

@ user2090226你的根目錄上有你的SecondPage.xaml嗎?只是問你,因爲人們犯了愚蠢的錯誤。否則,請嘗試使用uri完整路徑。最好檢查你的導航。您創建了一個對象而不是工廠方法。 例如:

new Uri("/MyProject;component/AllPageFolder/SecondPage.xaml",Urikind.Relative); 

對於在根目錄中創建自己拿這個例子頁數:

NavigationService.Navigate(new Uri("/Photography;component/SlideShow.xaml", UriKind.Relative)); 
1

我敢肯定你不能初始化new NaviagtionService(),你只能從PageInstance.NavigationService屬性來獲取一個實例,在Silverlight的。

(見http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice(v=vs.95).aspx

你可以使用(Application.Current.RootVisual as Frame).Content as PhoneApplicationPage當前頁面,這樣你就可以得到它PageInstance.NavigationService

但更簡單的方法是直接撥Navigate()Frame直接,(Application.Current.RootVisual as Frame).Navigate(...)將剛剛工作。

相關問題