Skip navigation

Understanding XAML Browser Applications (XBAP)

Executing Windows Presentation Foundation applications on the web browser

Related: "How to Calculate and Display Totals in XAML" and "Build and Deploy XAML Apps for the .NET Framework."

In Windows Presentation Foundation (WPF), you can have applications that fall under two distinct categories: standalone WPF applications and XAML browser applications (XBAPs). XBAPs can be used to create rich Internet applications (RIAs) applications that are hosted within a web browser. These applications combine the features of both web applications and rich-client applications. They can be published to a web server like web applications, then launched from the web browser. Like the rich-client applications, XBAPs can leverage the power of WPF. This article takes a look at what XBAPs are and what it takes to create an XBAP.

What are XBAPs?

As mentioned, XBAPs can be used for creating RIAs, which are hosted inside a web browser. Supported browsers include Internet Explorer (IE) and Firefox. Although you might see an XBAP running within a browser, the XBAP actually runs in the context of an out-of-process executable managed by .NET CLR. Note that in contrast to Windows applications, which are normally compiled to an executable (.exe) file, the XBAPs are compiled to files having .xbap extensions. These .xbap files can be executed in the context of the web browser. Also, these .xbap applications execute within a security sandbox to prevent untrusted applications from accessing the resources of the local system. MSDN states, "XAML browser applications (XBAPs) combines features of both Web applications and rich-client applications. Like Web applications, XBAPs can be published to a Web server and launched from Internet Explorer. Like rich-client applications, XBAPs can take advantage of the capabilities of WPF. Developing XBAPs is also similar to rich-client development."

XBAP and Silverlight

Both XBAP and Silverlight applications are used for creating RIAs, and both execute inside the context of a web browser. However, there are subtle differences between the two. Running an XBAP requires .NET Framework 3.0 and later to be installed on the system. Silverlight supports only a subset of XAML and is Microsoft's equivalent of Flash Player. Note that Silverlight is actually a browser component a browser plug-in. It is cross-platform and doesn't need .NET Framework 3.0 or later to be installed for it to work. Silverlight applications can be embedded in any browser and on any platform and rendered in the web browser using a Silverlight plug-in, whereas XBAPs can be executed on Windows platforms only.

Creating an XBAP

To create an XBAP, follow these steps:

1.     Open Visual Studio 2008 or Visual Studio 2010.

2.     Click New, Project.

3.     Select WPF Browser Application from the list of the project templates displayed.

4.     Specify a name for the project and click OK. You'll see a screen like this one:

Figure 1

Figure 1: Creating a WPF browser application

The WPF Browser Application project template creates an XBAP application project for you that includes the following elements:

         an application definition file called Application.xaml

         a page definition file called Page1.xaml

Next, open the Page1.xaml file and paste these lines between the tags:



Here's what the complete XAML code would look like:









Now compile the application. Doing so creates the .xbap file inside the Debug folder in the project's \bin directory. Finally, double-click the .xbap file to execute it. You're done! Here's what the output will look like:

Figure 2

Figure 2: Sample XBAP

Deploying an XBAP

When you compile a XBAP application, the following files are generated:

  • an executable file, with an .exe extension, that contains the compiled source code
  • an application manifest, with a .manifest extension, that contains the application's metadata information
  • a deployment manifest with an .xbap extension

You can easily publish your .xbap application to your web server. You just need to register the MIME types in your web server; your web server should have the following MIME types registered:

MIME Type

Extension

application/manifest

.manifest

application/x-ms-xbap

.xbap

application/octet-stream

.deploy

application/x-ms-application

application

application/vnd.ms-xpsdocument

.xps

application/xaml+xml

.xaml

Note that in some situations, you might see that after rebuilding and launching your XBAP, the previous version is launched that is, the latest version of the .xbap application you built isn't launched in the browser. The reason is that the cached version of the previous .xbap application is executed when the XBAP is requested. In such cases, you can remove the cached version from the cache by using the Manifest Generation and Editing Tool (mage.exe), as follows:

mage.exe -cc

Running this command removes the cached version of the XBAP from memory, so that only the latest version of the XBAP will be downloaded in the browser when requested.

Get Going with XBAP

As you've seen, XBAP applications are WPF applications that can be executed in the context of a web browser. We've explored the features of XBAP applications and how one can create a XBAP application using Visual Studio.

Joydip Kanjilal is lead architect for a company in Hyderabad, India, and is a Microsoft MVP in ASP.NET. He has authored Entity Framework Tutorial (Packt Publishing) and many other books and articles. Joydip blogs at aspadvice.com/blogs/joydip.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish