共用方式為


Getting Started: Navigation

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Adding navigation

iOS provides the UINavigationController class to help with navigation: you can push and pop view controllers to get the desired effect.

In Windows 8.1 and Windows Phone 8.1, navigation should blend into your app's content. This follows from one of the principles of Windows Store app design, "content before chrome". For more info, see Navigation patterns.

One of the ways to manage navigation is with the Frame class. The following walkthrough shows you how to try this out.

Continuing with the MyApp solution, open the MainPage.xaml file if it isn't already visible. Select either the MainPage under MyApp.Windows or MyApp.WindowsPhone — your choice. Add a button to the MainPage.xaml file in Design view. Change the button's Content property from "Button" to "Go To Page". Then create a handler for the button's Click event, as shown in the following figure. If you don't remember how to do this, review the walkthrough in the preceding section Getting Started: Getting around in Visual Studio (Hint: double-click the button in the Design view).

Let's add a new page, and place it in the MyApp.Shared branch so that it's available to both the Windows and Windows Phone versions of the app. Highlight MyApp.Shared in the Solution view, and then tap the Project menu, and tap Add New Item. Tap Blank Page as shown in the following figure, and then tap Add.

Next, add a button to the BlankPage.xaml file. Let's use the AppBarButton control, and let's give it a back arrow image: in the XAML view, add <AppBarButton Icon="Back"/> between the <Grid> </Grid> elements.

Now let's add an event handler to the button: double-click the control in the Design view and Microsoft Visual Studio adds the text "AppBarButton_Click" to the Click box, as shown in the following figure, and then adds and displays the corresponding event handler in the BlankPage.xaml.cs file.

If you return to the BlankPage.xaml file's XAML view, the <AppBarButton> element's Extensible Application Markup Language (XAML) code should now look like this:

<AppBarButton Icon="Back" Click="AppBarButton_Click"/>

Return to the BlankPage.xaml.cs file, and add this code to go to the previous page after the user taps the button.

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
    // Add the following line of code.    
    Frame.GoBack();
}

Finally, open the MainPage.xaml.cs file and add this code. It opens BlankPage after the user taps the button.

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Add the following line of code.
    Frame.Navigate(typeof(BlankPage1));
}

Now run the program. Tap the "Go To Page" button to go to the other page, and then tap the back-arrow button to return to the previous page.

Page navigation is managed by the Frame class. Similar to the UINavigationController class in iOS, which has pushViewController and popViewController methods, the Frame class for Windows Store apps has Navigate and GoBack methods. The Frame class also has a method called GoForward, which does what you might expect.

This walkthrough creates a new instance of BlankPage each time you navigate to it. (The previous instance will be freed, or released, automatically by the garbage collector). If you don't want a new instance to be created each time, add the following code to the BlankPage class's constructor in the BlankPage.xaml.cs file. This will enable the NavigationCacheMode behavior.

public BlankPage()
{
    this.InitializeComponent();
    // Add the following line of code.
    this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
}

You can also get or set the Frame class's CacheSize property to manage how many pages in the navigation history can be cached.

For more info about navigation, see Quickstart: Navigating between pages and XAML personality animations sample.

Note  For info about navigation for Windows Store apps using JavaScript and HTML, see Quickstart: Using single-page navigation.

 

Next step

Getting started: Animation