XAML Browser Applications XBAPs

While we're talking about Visual Studio tools for WPF, you may notice that a few icons away from the "Windows Application (WPF)" project template is another one called "XAML Browser Application (WPF)," as shown in Figure 1-8.

Figure 1-8. The WPF XAML Browser Application project template in VS05

WPF itself was created as a unified presentation framework, meant to enable building Windows applications with the best features from existing Windows application practice and existing web application practice. One of the nice things that web applications provide is a single window showing the user one page of content/functionality at a time, allowing for navigation among the pages. For some applications, including Internet Explorer, the shell Explorer, Microsoft Money, and a bunch of Control Panel applets, this is thought to be preferable to the more common Windows application practice of showing more than one window at a time.

To enable more of these kinds of applications, WPF provides the page, which is the unit of navigation in an XML Browser Application (XBAP). Instead of setting an application's StartupUri to a XAML file that defines a window, we point an XBAP's StartupUri at a XAML file that defines a page (Example 1-14).

Example 1-14. Starting with a Page instead of a Window

<!-- App.xaml --> <Application x:Class="MyFirstXbapApp.App"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Page1.xaml" />

A WPF page is a class that derives from the Page class, as shown in Example 1-15.

Example 1-15. A sample page

<!-- Pagel.xaml --> <Page x:Class="MyFirstXbapApp.Page1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowTitle="Pagel"> <TextBlock FontSize="36">

Check out <Hyperlink NavigateUri="page2.xaml">page 2</Hyperlink>, too. </TextBlock>

// Page1.xaml.cs namespace MyFirstXbapApp { public partial class Page1 : System.Windows.Controls.Page {

public Page1() { InitializeComponent();

The primary way to allow the user to navigate in an XBAP is via the Hyperlink element, setting the NavigateUri to a relative URL of another XAML page in the project. The first page of our sample XBAP looks like Figure 1-9.

Figure 1-9. A simple XBAP hosted in Internet Explorer 7

In Figure 1-9, the hyperlinked text is underlined in blue, and if you were to move your mouse cursor over the hyperlink, it would show up as red. Further, the page's WindowTitle property is set as the window caption. Of course, the most obvious thing to notice is that the XBAP is hosted inside the browser—Internet Explorer 7 to be exact. The reason for this is simple: XBAPs are meant to be deployed via the Web (which we'll talk about later in this chapter) and to blend seamlessly with web pages. As you navigate among the pages in an XBAP, those pages are added to the navigation history just as web pages would be, and you're allowed to use the Internet Explorer toolbar to go backward and forward, as you're used to doing.

XAML Browser Applications (XBAPs) | 15

For example, let's define page2.xaml as shown in Example 1-16.

Example 1-16. Another simple page

<!-- Page2.xaml --> <Page ... WindowTitle="Page2"> <TextBlock FontSize="36">

Hello and welcome to page 2. </TextBlock> </Page>

Clicking on the hyperlink on page 1 navigates to page 2, as shown in Figure 1-10.

$ Page2 - Windows Internet Explorer

$ Page2 - Windows Internet Explorer

® D:\data\AvalonBook\RtmSr - X Google P -

<J Current Page

# - Ü^Page^ ® Tools ^

+■ Pagel (Pagel.xarril) <& History Ctrl+ShifttH

welcome to page 2.

4. Computer | Protected Mode: Off

Figure 1-10. XBAP and navigation history

Notice in Figure 1-10 that the history for the back button is showing page 1, which is where we were just before getting to page 2.

As you might imagine, there are many more topics to discuss to make your XBAPs integrate with the browser and still provide the rich functionality we expect from WPF applications. In addition, you can have any number of navigation windows in your standalone WPF applications. We cover these topics and more in Chapter 11.

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