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。请建议任何替代方式。提前致谢。

完整代码如下:

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 中初始化 a 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

@user2090226 你的 SecondPage.xaml 在根目录下吗?只是问你,因为人们犯了愚蠢的错误。否则尝试使用 uri 进入完整路径。最好检查您的导航。您创建了一个对象而不是工厂方法。前任:

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

对于在根目录中创建的页面,请使用以下示例:

NavigationService.Navigate(new Uri("/Photography;component/SlideShow.xaml", UriKind.Relative));
于 2013-04-17T08:14:29.307 回答
0

我的心理调试能力告诉我,您已经在其中编写了代码: - 静态构造函数 - 构造函数 - 在调用之前调用的方法OnNavigatedTo

一般来说,NavigationServicenull在你输入之前的OnNavigatedTo方法。您可能希望将该逻辑从其当前位置移动。

无论如何,它现在位于哪里?

于 2013-04-16T14:12:57.470 回答