0

私のアプリケーションは、mainpage.xaml からユーザー名とパスワードを収集し、サーバーに httprequest を作成します。サーバーからの応答が PASS の場合、コントロールを別の xaml ページにナビゲートします。次のコードを使用しました

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



    }
}
4

3 に答える 3

1

Silverlightでは、プロパティnew NaviagtionService()からのみインスタンスを取得できます。PageInstance.NavigationService

( 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(...)ことです。

于 2014-08-28T19:00:49.957 に答える
0

私の精神的なデバッグ能力は、あなたがコードを次のいずれかに記述したことを示しています: - 静的コンストラクター - コンストラクター - 呼び出す前に呼び出されるメソッドOnNavigatedTo

一般に、メソッドNavigationServicenull入る前ですOnNavigatedTo。そのロジックを現在の位置から移動したい場合があります。

とにかく今どこにありますか?

于 2013-04-16T14:12:57.470 に答える