0

私のアプリケーションはユーザー名とパスワードを取得し、ハイパーリンク ボタンをクリックすると、これらの値がサーバーに送信されるため、サーバーは PASS:ClientID のような値を返します。responseString に PASS が含まれている場合にのみ、(MainPage.xaml.cs から) SecondPage.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 が発生します。

エラーを見つけて修正を提案するのを手伝ってください。

前もって感謝します

4

1 に答える 1

0

NavigationService が resource を見つけられないため、エラーが発生する可能性が最も高いです/SecondPage.xaml。SecondPage はプロジェクトのルートにありますか?

これは、ターゲット リソースが読み込まれる前に移動しようとすることによっても発生する可能性があります (たとえば、ページのコンストラクター内を移動することによって) が、それがすぐに問題になるわけではありません。

この回答は、名前空間またはアセンブリ名を変更した後にこの問題が発生する可能性があることを示唆しています。プロジェクトをクリーニングし、すべての bin フォルダーと obj フォルダーが空であることを確認してから、再コンパイルすると修正されると記載されています。ただし、その参照リンクは無効です。

于 2013-04-17T13:56:39.263 に答える