简体   繁体   English

在Windows Phone 7中,导航服务特定于页面。 如何从外部页面调用?

[英]In windows phone 7, navigation service is specific to a page. How do i invoke from an outside page?

My application collects username and password from mainpage.xaml and makes an httprequest to the server. 我的应用程序从mainpage.xaml收集用户名和密码,并向服务器发出httprequest。 If the response from the server is PASS then i want the control to be navigated to another xaml page. 如果来自服务器的响应是“通过”,那么我希望将控件导航到另一个xaml页面。 I used the following code 我用下面的代码

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

where rs1 is an array of strings. 其中rs1是字符串数组。

But I get a NullReferenceException. 但是我得到了一个N​​ullReferenceException。 Plz suggest any alternate way. 请提出其他替代方法。 Thanks in advance. 提前致谢。

The full code is as follows: 完整的代码如下:

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



    }
}

I'm pretty sure you cannot initialize a new NaviagtionService() , you can only get an instance from PageInstance.NavigationService property, in Silverlight. 我非常确定您无法初始化new NaviagtionService() ,只能从Silverlight中的PageInstance.NavigationService属性获取实例。

(See http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice(v=vs.95).aspx ) (请参阅http://msdn.microsoft.com/zh-cn/library/system.windows.navigation.navigationservice(v=vs.95).aspx

You can get the current page using (Application.Current.RootVisual as Frame).Content as PhoneApplicationPage , so you can get PageInstance.NavigationService on it. 您可以使用(Application.Current.RootVisual as Frame).Content as PhoneApplicationPage来获取当前页面,因此可以在其上获取PageInstance.NavigationService

But a simpler way is to call Navigate() on Frame directly, (Application.Current.RootVisual as Frame).Navigate(...) will just be working. 但是更简单的方法是直接在Frame上调用Navigate() (Application.Current.RootVisual as Frame).Navigate(...)才可以工作。

My psychic debugging abilities tell me that you have wrote the code inside either: - a static constructor - constructor - a method called before calling OnNavigatedTo 我的心理调试能力告诉我,您已经在其中之一内编写了代码:-静态构造函数-构造函数-在调用OnNavigatedTo之前调用的方法

In general, NavigationService is null before you enter the OnNavigatedTo method. 通常,在输入OnNavigatedTo方法之前, NavigationServicenull You might want to move that logic from its current position. 您可能希望将该逻辑从当前位置移开。

Where is it located now anyway? 反正现在在哪里?

@user2090226 Do you have your SecondPage.xaml on the root? @ user2090226根目录上是否有SecondPage.xaml? Just asking you because people make silly mistakes. 只是问你是因为人们犯了愚蠢的错误。 Otherwise try using uri to be in full path. 否则,请尝试使用uri进行完整操作。 And preferably check your navigation. 最好检查一下您的导航。 You created an object instead of factory methods. 您创建了一个对象而不是工厂方法。 Ex: 例如:

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

For Pages created in the root itself take this example: 对于在根目录本身中创建的页面,请使用以下示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM