windows phone navigation immediately after loading

2019-02-21 01:21发布

问题:

I have 2 pages.(MainPage.xaml,second.xaml) MainPage.xaml is the Login page. In this page I send login and password, and receive result. I save them(result) in Isolate Storage and navigate to the second.xaml page; When i start this application in the next time, i extract data from Isolate Storage and I want to navigate the second.xaml immidiately, but i don't know how

I try write

public MainPage()
    {
        InitializeComponent();

       //function for Isolate storage
        InitializeSettings();
        NavigationService.Navigate(new Uri("/Conversation.xaml", UriKind.Relative));
    }

But it isn't work) I understood that I could not use the navigation code associated with the MainPage() constructor. Of course, i may will do simple button, but i wish fast navigation

I think may be it connected with App.xaml method

private void Application_Launching(object sender, LaunchingEventArgs e)

for example, write my method

        //function for Isolate storage
        InitializeSettings();

with navigation there?(navigation not work in this example)

private void Application_Launching(object sender, LaunchingEventArgs e)
{
InitializeSettings();
 NavigationService.Navigate(new Uri("/Conversation.xaml", UriKind.Relative));
}

Where I can use navigation, so go straight to the second.xaml page, without fully loading the MainPage.xaml(may be without MainPage.xaml)

回答1:

You can do as Rana Tallal said.

Or you can write it in code:

public MainPage()
{
    InitializeComponent();

    Loaded += (s, e) =>
    {
        InitializeSettings();

        // Some login-password check condition
        if (_login && _password)
            NavigationService.Navigate(new Uri("/Conversation.xaml",
                                               UriKind.Relative));
    }
}


回答2:

Well create a new function... and in that perform the checks at which you want it to be navigated and if the checks are ok right in it than call the navigation service navigationservice.navigate(....) code. Now you need to tell the program to call this function when the mainpage is completely loaded. To do so in the xml of mainpage inside tags at the end of it write loaded="function_name" Now when ever the page will be loaded this function will be called. If the login information is present in the isolated storage than the navigation sevices will be called otherwise the mainpage will be shown.

Make sure to put (object sender, RoutedEventArgs e) in the functions parameters(as it is a event handler).