The two core components of the navigation framework are the Frame control and the Page
class. There are also two other helper objects that enable you to navigate between views and get information about a navigation operation—the NavigationService
class and the NavigationContext
class. Let's take a look at each of these components and what roles they play in the navigation framework.
The Frame control is a host container for views. Therefore, it is normally used in a master page that provides the outer shell of the application and hosts the views as content. When provided with a URI specifying the view to load and display, it will pass that URI through a converter to obtain a path to the view that it can then navigate to.
The Frame control also provides browser journal (history) integration and handles navigation events raised by the browser, such as clicking the Back and Forward buttons, entering new URIs via the address bar, and so on.
In the project you created from the Silverlight Business Application project template, the MainPage.xaml
file, acting as the application shell, contains a Frame control, enabling it to host the views.
Views must inherit from the Page
class, which enables them to be hosted in a Frame control.
Note There's somewhat of an inconsistency in the use of terminology within the navigation framework (pages vs. views), which should be clarified to avoid confusion. In the Model-View-ViewModel (MVVM) design pattern discussed in Chapter 13, I refer to each of these “content” pages as a “view.” In the default Silverlight Business Application project template these views reside in the Views
folder, which makes perfect sense. However, the views inherit from the Page
class, and you use the Silverlight Page item template when adding a new view to your project. The term “page” tends to conjure up the image of a web page, which is not really a valid comparison. From its name, you might also expect the MainPage
class to inherit from Page
, but it actually inherits from UserControl
. Ideally, the Page
class would have been named View
instead, which would have solved this inconsistency. However, this would put it out of alignment with WPF's navigation framework. If this bothers you, you might consider creating a class called View
that inherits from Page
, which you can use in Page
's place. Generally, it's also a good idea to suffix the name of each view with View
—for example, HomeView
, AboutView
, and so forth. Throughout this book, when I mention the term “view,” it will generally be in reference to one of these XAML files that inherits from Page
.
In the project you created from the Silverlight Business Application project template, you will find that the Home.xaml
and About.xaml
files in the Views
folder both inherit from the Page
class.
You can create additional views by using the Silverlight Page item template when adding a new item to your project.
Note On each view, ensure that you set the Title
property, which is inherited from the Page
class, to the name of that view. The value of this property will be displayed in the browser's title bar and in the history when the view has been navigated to.
Each Frame control instance creates a NavigationService
object that is shared with the views it hosts, accessible via a property on the Page
class each view inherits from. This NavigationService
object is used to maintain an independence between the views and their host (the Frame control) and contains the methods that enable you to programmatically navigate around the application.
The NavigationContext
object contains information about the navigation operation that led to the current view being displayed, which amounts to the query string appended to the URI.
You define the layout/structure of your user interface (outer shell) in the MainPage
class, and this generally provides the means to navigate between the views. The MainPage
class inherits from UserControl
, and each view inherits from the Page
class, which is a part of the navigation framework. By inheriting from the Page
class, the views can be hosted within a Frame control and can handle the various navigation events provided by the navigation framework.