Charles Petzold



Converting Non-Loose XAML to XBAP

August 19, 2007
Roscoe, N.Y.

In some recent blog entries, I've shown some XAML files that reference the Petzold.Media3D library. You can also find a bunch of these "non-loose" XAML files in the Figures directory along with the downloadable code for my new book 3D Programming for Windows.

I created these files in XamlCruncher 2.0. If you'd like to run these XAML files outside of XamlCruncher 2.0, they must be converted to EXEs or XBAPs. Converting to an XBAP is the easier option. Here's how:

Suppose you have a XAML file named MyMasterpiece.xaml that references a DLL named MyLibrary.dll. In Visual Studio, create a new project of type XAML Browser Application named MyMasterpiece.

Delete the files Readme.txt, Page1.xaml, Page1.xaml.cs, and App.xaml.cs.

From the Project menu, select Add Existing Item (or right-click the Project name in the Solution Explorer and select Add and Existing Item). Add MyMasterpiece.xaml to the project. You can either copy the file to the project directory or just add a link to it.

From the Project menu, select Add Reference. Click the Browse tab. Navigate to MyLibrary.dll and select it.

Edit the App.xaml file. Delete the x:Class attribute. For the StartupUri attribute, replace Page1.xaml with MyMasterpiece.xaml. Select Save from the File menu to save the new version. (This shouldn't be necessary but it sometimes it.)

You can now compile and run MyMasterpiece.xbap.

If Visual Studio complains it can't find the executable after compiling it, it probably screwed up the .CSPROJ project file. Right click the project name in the Solution Explorer and select Unload Project. Right click the project name again and select Edit MyMasterpiece.csproj. Make sure the ApplicationExtension tag comes before the StartArguments tag. (That's usually the problem.) Right click the project name again and select Reload Project. You'll be prompted to save the project file. Do so.

Once you have compiled an XBAP, it is extremely easy to publish it to your Web site: Select Properties from the Project menu, click the Publish tab, and in the Publish Location enter an FTP address you would normally use to copy files to your site. The FTP address can include a new subdirectory (commonly the name of the project). Click Publish Now. You will be prompted for a login name and password.