Understanding XAML Browser Applications (XBAP)

Executing Windows Presentation Foundation applications on the web browser

Joydip Kanjilal

October 30, 2009

4 Min Read
ITPro Today logo

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 canhave applications that fall under two distinct categories: standalone WPFapplications and XAML browser applications (XBAPs). XBAPs can be used to createrich Internet applications (RIAs) applications that are hosted within a webbrowser. These applications combine the features of both web applications andrich-client applications. They can be published to a web server like webapplications, then launched from the web browser. Like the rich-clientapplications, XBAPs can leverage the power of WPF. This article takes a look atwhat 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 InternetExplorer (IE) and Firefox. Although you might see an XBAP running within abrowser, the XBAP actually runs in the context of an out-of-process executablemanaged by .NET CLR. Note that in contrast to Windows applications, which arenormally compiled to an executable (.exe) file, the XBAPs are compiled to fileshaving .xbap extensions. These .xbap files can be executed in the context ofthe web browser. Also, these .xbap applications execute within a securitysandbox to prevent untrusted applications from accessing the resources of thelocal system. MSDN states, "XAML browser applications (XBAPs) combinesfeatures of both Web applications and rich-client applications. Like Webapplications, XBAPs can be published to a Web server and launched from InternetExplorer. Like rich-client applications, XBAPs can take advantage of thecapabilities of WPF. Developing XBAPs is also similar to rich-clientdevelopment."

XBAP and Silverlight

Both XBAP and Silverlight applications are used forcreating RIAs, and both execute inside the context of a web browser. However,there are subtle differences between the two. Running an XBAP requires .NETFramework 3.0 and later to be installed on the system. Silverlight supportsonly a subset of XAML and is Microsoft's equivalent of Flash Player. Note thatSilverlight is actually a browser component a browser plug-in. It is cross-platformand 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 andrendered in the web browser using a Silverlight plug-in, whereas XBAPs can beexecuted on Windows platforms only.

Creating an XBAP

To create an XBAP, follow these steps:

1.     OpenVisual Studio 2008 or Visual Studio 2010.

2.     ClickNew, Project.

3.     SelectWPF Browser Application from the list of the project templates displayed.

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

Figure 1: Creating a WPF browser application

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

        an application definition file calledApplication.xaml

        a page definition file called Page1.xaml

Next, open the Page1.xaml file and paste theselines 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 willlook like:

Figure 2: Sample XBAP

Deploying an XBAP

When you compile a XBAP application, the followingfiles 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 toyour 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 thatafter rebuilding and launching your XBAP, the previous version is launched thatis, the latest version of the .xbap application you built isn't launched in thebrowser. The reason is that the cached version of the previous .xbapapplication is executed when the XBAP is requested. In such cases, you canremove the cached version from the cache by using the Manifest Generation andEditing Tool (mage.exe), as follows:

mage.exe -cc

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

Get Going with XBAP

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

Joydip Kanjilal is leadarchitect 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.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like