Converting EXE Projects to XBAPs
November 12, 2006
Roscoe, N.Y.
In a recent post, Rob Relyea contemplates the question “Are XBAPs next generation ActiveX?”
That question is way too metaphysical for my tastes, but I do know that sometimes a developer might want to convert a WPF project from generating an EXE to generating an XBAP. That’s what this blog posting is all about.
Some background for newcomers:
The Windows Presentation Foundation supports three distinct types of executables. First, there is the traditional EXE file, which can do anything that .NET allows it to do plus more through interoperability with the Win32 API.
Secondly, there are XAML files. These are text markup files with the extension .XAML, and they run in Internet Explorer. Although XAML files are quite powerful graphically—they can do 3D animation, for example—they cannot contain any C# code for the often-necessary chores of handling events from controls.
And thirdly, there is the XBAP. (This stands for “XAML Browser Application.” In my head I pronounce it as “eks bap” which makes it sound rather silly. At one time, these things were known as XAPP’s, which sounds rather hipper to me.) XBAP applications are somewhere between EXEs and XAML files. They can contain code as well as markup, and they are compiled, but they run in Internet Explorer much like XAML files. These applications cannot contain elements of type Window. They use Page instead, and they are run in a sandbox that prohibits from messing with the user’s machine in devious ways. XBAPs generally run over the Web and are generally not installed on local machines.
In Visual Studio you choose rather you want an EXE or an XBAP when you first create a project. From the New Project dialog box you pick either “Windows Application (WPF)” or “XAML Browser Application (WPF)”.
Sometimes you’re developing an EXE application and you realize “Hey, I’m not doing anything in the app that will prevent it from running in the browser as an XBAP so maybe I want to go that route.”
Or, perhaps you prefer developing an application as an EXE because of better debugging facilities, and then you want to convert it to an XBAP for distribution.
Of course, you’ll probably need to change several aspects of your application, and in particular, you’ll need to change Window and NavigationWindow to Page. But that’s not really what I want to discuss here. I want to talk about changes you’ll need to make in the.CSPROJ project file, and for this I was helped by the documentation topic “How to: Create a Sample Project File for a Windows Presentation Foundation XAML Browser Application” which is located in the documentation at:
-
.NET Framework Development
Windows Presentation Foundation
Application Development
Build and Deploy
How-to Topics
Some of the changes you need to make to the .CSPROJ file can be made by selecting various options in the Project Properties pages, which you can invoke by right-clicking the project name in the Solution Explorer and selecting Properties, or by selecting Properties from the Project menu.
However, you’ll also have to edit the .CSPROJ file manually, and you can do this directly in Visual Studio. First, right click the project name in the Solution Explorer and select Unload Project. Or select Unload Project from the Project menu. Then, right-click the project name and select the option to Edit the .CSPROJ file. When you’re done editing it, you can save it, or just select Reload Project and you’ll be prompted to save the edited project file. (Basically, you can’t be editing the .CSPROJ file and doing anything else with the project at the same time.)
Here are the steps to convert a project from EXE to XBAP (after you create a backup copy first, of course):
Step 1. Edit the .CSPROJ file and insert the following lines in the first Project/Property Group section. You can insert them right after the OutputType element, for example:
-
<HostInBrowser>true</HostInBrowser>
<StartAction>Program</StartAction>
<StartProgram>$(WINDIR)\System32\PresentationHost.exe</StartProgram>
<ApplicationExtension>.xbap</ApplicationExtension>
<StartArguments>-debug "$(MSBuildProjectDirectory)\bin\$(Configuration)\$(AssemblyName)$(ApplicationExtension)"</StartArguments>
The StartArguments element should be written as a single line because it denotes a directory path. (IE seems to want to break the long line after an embedded right parenthesis.) Also, this is XML so remember: Case matters! Also, make sure the ApplicationExtension element goes before the StartArguments element, because it refers to the ApplicationExtension element.
The next three steps can be done with the Project Properties pages.
Step 2. Select the Signing tab. Check the Sign The ClickOnce Manifests option. Click the Create Test Certificate button. Just click OK without entering a password. (This creates a temporary manifest key that is referenced in the project file.)
Step 3. Select the Security tab. Make sure the Enable ClickOnce Security Settings option is checked. Select the This Is A Partial Trust Application option. In the Zone Your Application Will Be Installed From drop-down, select Internet. (This changes the TargetZone element in the project file to Internet.) Click the Advanced button. Uncheck Debug This Application With the Selected Permissions Set.
Step 4. Select the Publish tab. Check the The Application Is Available Online Only option. (This changes the Install element in the project file to false.)
Step 5. Unload the project and edit the .CSPROJ file again. You’ll probably see the ApplicationExtension element has been moved down near the bottom of the first Project/PropertyGroup section. Move it back so it comes before the StartArguments element.
Step 6. In the References section under the project in the Solution Explorer, delete the following:
-
System.IdentityModel
System.Runtime.Serialization
System.ServiceModel
And that should do it. (And I suspect that the next version of Visual Studio will reduce this entire job to a single check box.)