Creating XBAP Poppers
Alvin Bruney shows us how to create XBAP Poppers.
October 30, 2009
asp:feature
Creating XBAP Poppers
By Alvin Bruney
If it s indeed true that a picture is worth a thousandwords, I should be able to save some space in this article with a high impactpicture right? Look at Figure 1. Would you believe me if I told you that Figure1 is a .NET browser-based application? It is! And I ll prove that it s not amock-up by showing you how to produce one just like it. Go ahead and download acopy of PaintShop Pro just in case this experiment doesn t work out as planned,that way we can do a quick mock-up to save face.
Figure 1: XBAP behaving like aWindows application.
After examining Figure 1 closely, I m sure you re dying toask some questions. For instance, where is the browser? It s in the Windowstaskbar, and the taskbar is set to auto-hide.
How does it work? When I close the browser, theapplication closes. I can use the browser to perform other browser-relatedactivities without interfering with the application. Or, I can use theapplication without disturbing the browser. I can also use both applications tosuite my needs.
Does this work in other browsers? Sure, all major browsersare supported. In fact, Figure 1 uses Firefox version 2.0.
Let s review some theory. XBAPs, or browser-basedapplications, are applications built on WPF that are hosted in a browser. Forthese types of applications, .NET 3.x is required on the client. Security isscoped to the hosting container in what is commonly termed the browser sandbox.There are a few limitations for these types of applications. Review http://msdn.microsoft.com/en-us/library/aa970060.aspxfor more details on the XBAP story. For now I ll skip all that stuff and getright to it.
Let s begin by creating an application based on a standardWPF Browser Application template (see Figure 2) by selecting New | Project. Puta button on the form and wire the click event to show a MessageBox when thebutton is clicked.
Figure 2: Creating a browser-basedapplication in WPF.
Let s examine the XAML file to see what s going on. Hereis the complete XAML code:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Page1"> VerticalAlignment="Top"Click="button1_Click">Button
Now it s time for the hack. To get the application to popout of the browser (an XBAP Popper), derive the document from a Window insteadof a Page object by replacing the start and end Page tags in the Page1.xamlfile with Window tags. Your XAML file should now look like this:
Extras removed forclarity
Then, navigate to the code-behind for Page1.xaml.cs andadjust the inheritance from public partial class Page1 : Page to public partialclass Page1 : Window.
If you try to run the application, you ll get a securityexception informing you that the assembly needs to be signed. Makes senseright? Full trust is required to satisfy the demand for UIPermission.AllWindowswhen the browser tries to create a Window. Let s fix this. From the propertieswindow, add the settings according to Figure 3.
Figure 3: XBAP signing tab.
Select the Security tab and choose the first option to runthe application in full trust (see Figure 4).
Figure 4: Enabling full trust in anXBAP.
Next, import the certificate into the Trusted Publisherstore. The easiest way to do this is to open your application directory andright-click on the PFX file. Choose Install PFX as shown in Figure 5.
Figure 5: Option to install PFX.
The Certificate Import Wizard will appear. Walk throughthe wizard and install the certificate into the Trusted Publishers store; see Figure6 for the important step in the wizard process.
Figure 6: Importing a certificateinto the Trusted Publishers store.
The wizard should conclude with a success prompt. Run theapplication. When the application is completely downloaded, the Hello Worldapplication will begin (see Figure 7).
Figure 7: XBAP Popper.
That s it. And you didn t even need the PaintShop Pro!
Are you disappointed that Figure 7 doesn t resemble Figure1? That s because I ve added some eye-candy to the application. To jazz up thedemo like Figure 1, follow these steps:
Change the default Window appears to supporttransparency (AllowsTransparency = True in page1.xaml).
If you re on a budget like me, download andinstall the free Xceed Datagrid from http://xceed.com/Grid_WPF_Intro.htmland set the theme to glass. Load some test data into the grid.
Add buttons to the form. The transparent Windowwill create the effect of floating buttons as shown in Figure 1.
Add a Text region with a transform so that itappears angled. This creates the Drag Handle at the bottom of the Window in Figure1.
Handle the click event for the Text region soyou can drag the Window.
Add a spinning bitmap effect customized fromMSDN (http://msdn.microsoft.com/en-us/library/ms753347.aspx).
Auto-hide the task bar in Windows.
If you get stuck along the way, download the completepage1.xaml code.
To sum up, you should understand that XBAPs run in abrowser and can utilize 98% of WPF. But they do have a few disadvantages. Theentire application must be downloaded before a single byte will run. This isunsuitable for some types of applications. For instance, critical line ofbusiness applications that are used by a large number of people at roughly thesame time during the day may cause performance issues as servers struggle topush the code to the users at the same time.
Some XBAPs require full trust and certification management,as we ve seen. For large companies, this added overhead is significant. XBAPPoppers run in a browser, which means familiarity benefits for most users. Ifyou can accept and manage these challenges, then go ahead and dazzle peoplewith an XBAP Popper!
The files referencedin this article are available for download.
Alvin Bruney is aTechnology Specialist working for Royal Bank of Canada in the .NET Centre ofExcellence program. He is a Microsoft Press author and a long-time ASP.NET MVP.
About the Author
You May Also Like