Pages

If we want to get a little fancier than a string, we can create multiple "pages" of content, which is specifically what the Page class was invented for (see Example 11-2).

Example 11-2. Packaging content with a Page object

x:Class="NavigationBasics.Page1" Title="Page 1"

WindowTitle="Welcome to Page 1">

<TextBlock VerticalAlignment="Bottom"> <Hyperlink NavigateUri="Page2.xaml">Click to see page 2</Hyperlink>

// Page1.xaml.cs using System.Windows.Controls; // home of the Page public partial class Page1 : Page { public Page1() { // Initialize page from XAML InitializeComponent();

To get the basic skeleton of a new Page class, you can right-click on your project in the Visual Studio 2005 Solution Explorer, choosing Add ^ New Item, and select Page (WPF). Example 11-2 was started that way, adding the WindowTitle, the Title, and the content. The WindowTitle is what shows up in the caption of the navigation host. The Title property is what shows up in the history drop-down. If you don't set a page's Title property, it will be composed for you as WindowTitle (foo.xaml), which isn't particularly friendly.

The content in Example 11-2 uses a Hyperlink, which is a nice little element that handles clicking for navigation applications.* We're setting the NavigateUri property to point to the page resource we'd like it to load for us. The NavigateUri supports the normal URI format (e.g., a URL to an HTTP file on the Web), as well as the pack URI format described in Chapter 12. Page two of our content is another custom Page class defined in XAML, as shown in Example 11-3.

Example 11-3. Using the navigation service

x:Class="NavigationBasics.Page2" Title="Page 2"

WindowTitle="Welcome to Page 2"> <Button Name="backButton"

VerticalAlignment="Center" HorizontalAlignment="Center"> Click to go back to page 1 </Button> </Page>

// Page2.xaml.cs public partial class Page2 : Page { public Page2() { InitializeComponent();

// Handle the button Click event backButton.Click += backButton_Click;

void backButton_Click(object sender, RoutedEventArgs e) { // The Page class provides direct access to navigation services this.NavigationService.GoBack();

Example 11-3 looks pretty much like Example 11-2, except that in this case, we're assigning the hyperlink a name in the XAML so that we can handle the Click event and handle the "go back" navigation as though the user had clicked the Back button (which is enabled as soon as the user has navigated to another page).

* We describe the Hyperlink element and its role as part of the WPF text object model in Chapter 14.

To navigate programmatically, each navigation host provides a navigation service. The navigation service is responsible for fulfilling navigation requests, tracking history, providing events for handling navigation events (e.g., Navigating, Navigated, NavigationFailed, etc.), as well as methods of navigating history (e.g., GoBack, GoForward, Navigate, etc.).* To access the navigation service associated with a dependency object, you can use the static GetNavigationService method of the NavigationService class:

void backButton_Click(object sender, RoutedEventArgs e) { // get the page's navigation service NavigationService navService = NavigationService.GetNavigationService(this); navService.GoBack();

As a shortcut, the Page class provides the NavigationService property. In addition, the Page class supports the set of navigation commands (as described in Chapter 4) on the NavigationCommands class (e.g., BrowseBack, BrowseForward, Refresh, etc.). You can use the commands to eliminate the need for any Click event handler code in our example Page 2, as shown in Example 11-4.

Example 11-4. Using navigation commands

x:Class="NavigationBasics.Page2" Title="Page 2"

WindowTitle="Welcome to Page 2"> <Button Command="NavigationCommands.BrowseBack"

VerticalAlignment="Center" HorizontalAlignment="Center"> Click to go back to page 1 </Button> </Page>

// Page2.xaml.cs public partial class Page2 : Page { public Page2() { InitializeComponent();

With our content hosted in pages, we can use the URI trick, shown in Example 11-5, to navigate to the first page from the navigation window.

* For a wonderful picture of the navigation events and when they happen, I recommend the SDK topic "Navigation Overview," available at http://msdn2.microsoft.eom/en-gb/library/ms750478.aspx#NavigationService (http://tinysells.com/92).

Example 11-5. Navigating to the first page from the main window // Window1.xaml.cs public partial class Window1 : NavigationWindow { public Window1() { InitializeComponent();

// Show first page this.Navigate(new Uri("Page1.xaml", UriKind.Relative));

In fact, the desire to define an entire application as a set of pages and to simply navigate to the first page without any muss or fuss is something that the Application object's StartupUri property supports directly, removing the need for a main window to host page content at all (see Example 11-6).

Example 11-6. Navigating to the first page using the StartupUri <!-- App.xaml -->

<Application ... StartupUri="Page1.xaml">

<!-- no need for a main window at all -- > </Application>

In the case of a standalone Windows application, the application will create a NavigationWindow for you and navigate to the page specified by the StartupUri property, as Figure 11-2 shows (after we've navigated to the second page).*

I«7! Welcome to Page 2 ° H I* 5 'I

>/ Current Page

□ism

Click to go back to page 11

Figure 11-2. Populating the history with the Title property

Notice that setting the Title property on each Page has resulted in the name of the page instead of the WindowTitle property.

* In the case of an XBAP, the application will not create a NavigationWindow, as it doesn't have the permissions in partial trust to do so. Instead, it will create another navigation host that knows how to show your pages just like a page of HTML in Internet Explorer 6+, as you'll see later, in the "XBAPs" section.

Although setting the StartupUri property is a useful shortcut if your application's main window is going to be navigation-based, nothing is stopping you from using NavigationWindow-like dialogs to build wizards, even if your main window is not navigation-based. The WPF factoring of NavigationWindow allows it to be used like any other window.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment