What is XAML?

If you haven’t yet heard of XAML then you need to get out more. This post assumes that most have at least a vague understanding of what it is and its potential impact moving forward. If you’re not hip to XAML then do a few Google searches, visit a site like XAML.NET, or get some info on XAML from the horse’s mouth. In any event this post is not designed to get into the nitty-gritty of the who, what, when, or where of XAML.

XAML Visual FoxPro Style

About 9-10 months ago I created a .NET ActiveX control that will allow any COM-aware language (such as Visual FoxPro) to host XAML controls. Internally this .NET control uses the ElementHost class (as in System.Windows.Forms.Integration.ElementHost) to provide for a UserControl (as in System.Windows.Controls.UserControl) to reside within a UserControl (as in System.Windows.Forms.UserControl). Confused yet? In any event, basically what it allows a developer to do is run XAML where it wasn’t necessarily intended or designed to run in the first place (such as placing XAML controls on a Visual FoxPro form).

If you want more information on how to create a .NET ActiveX control for use in Visual FoxPro then head on over to my previous blog post regarding the subject. This post is neither going to explain XAML nor teach you how to make a .NET ActiveX control. So what am I going to do? Well I’m going to show you some screen shots of XAML running inside a Visual FoxPro form, give you the ActiveX control along with the C# source, give you a Visual FoxPro example (that’s where the screen shots are going to come from), and I’m going to tell you to make sure you are running Windows XP SP2 or higher and have you go install the .NET Framework 3.0 (formerly known as WinFX) so you can run the Visual FoxPro example and perhaps even impress your friends and colleagues. Not a bad deal right?

Visual FoxPro XAML Example

Included in the download at the bottom of this page is a form with 4 examples of XAML running in Visual FoxPro. Not only does the example show you one of the ways to host XAML controls in a COM-aware language like Visual FoxPro, but it also allows you to dynamically change aspects of the XAML controls (or delete them, or add new ones, etc.). The XAML can be modified while the form is running and reloaded via the provided button. I could have just as easily been done it in code or by pulling XAML from a table or whatever – XAML is just XML after all.

Below are some screen shots of the 4 examples running on Windows XP SP2 and Visual FoxPro 9.0. Does it work with earlier versions of Visual FoxPro? For those of you asking me that question in your heads right now I would urge you to upgrade to Visual FoxPro 9.0 and then tell you that if your version of Visual FoxPro can host an ActiveX control then it will work.

Example 1

Example 2

Example 3

Example 4

What’s XAML and What’s Not

Everything above the label that has a caption reading “Enter XAML UserControl below” is XAML. Everything below and including the label is Visual FoxPro. Now, be forewarned that there is a pretty serious lag when you click on the Load XAML button for the first time. After that the examples come up pretty good. The initial lag is due to the overhead of loading the necessary 3.0 .NET Framework parts into memory. After that any lag is due to the number of abstraction layers necessary to make this example possible.

If these XAML examples look familiar then you either attended Southwest Fox 2006’s keynote or you saw them on another site because I am not the original author of the XAML included in these examples. And yes, proper credit and links have been given to the original authors (where known) in the comments provided in the form’s code.

How to Get the Example Up and Running

When you’ve extracted the zip you will see two folders: VFPXAML and VFP Example. The former contains the code for the C# solution provided in the root folder of the zip – this would be the ActiveX control. The latter contains a Visual FoxPro form named “Form1” that you can run when you have installed the latest 3.0 .NET Framework and registered the ActiveX control (…\VFPXAML\VFPXAML\bin\Release\VFPXAML.dll). To register the ActiveX control simply use the RegAsm.exe that is provided with .NET (have a look in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\). The command you’ll want to execute in DOS looks a little something like this…

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe /codebase C:\MyPath\VFPXAML\VFPXAML\bin\Release\VFPXAML.dll

More information regarding RegAsm.exe can be found on the Microsoft site for those of you that aren’t familiar with it. Just think of it as the equivalent of Resvr32 when it comes to .NET.

Next, run the previously mentioned form1 in Visual FoxPro, select the example you want via the provided optiongroup in the bottom left of the form and when ready click Load XAML to run the example. As I said before, the XAML can be changed. Simply modify the XAML in the provided editbox and the re-click Load XAML. Nearly anything that can be rendered as a XAML usercontrol can be used. Mess around and see what you can come up with. If you create a really cool XAML example then blog about it and share it with the Visual FoxPro community. If nothing else, send it to me and I’ll blog about it.

Is This Production Ready?

Hardly. It is just a simple prototype that I whipped together and admittedly it hasn’t been touched much in the last 9 months. I just wanted to show that this sort of thing was possible and hopefully get Microsoft to sign off on a XAML project for Sedna. Ultimately it is designed to show one of the ways that developers can access new technologies that aren’t natively supported by COM-aware languages by using .NET. There are certainly other ways to go about this sort of thing. One method, which has less overhead is to forego the Visual FoxPro form and do the entire thing on a WinForm in .NET. The WinForm can be controlled by Visual FoxPro via COM interop and interfaces can be created to give you access to the WinForm’s properties, events, and methods.

Is there an easier way to produce interop controls and forms in .NET for use in Visual FoxPro? Sure. Instead of writing all of the tedious interoperability code yourself you could take advantage of the Interop Forms Toolkit 2.0 from Microsoft. It should also be pointed out that VB.NET has some advantages over C# when it comes to interoperability (although VB.NET won’t impress your friends and colleagues nearly as much). One of the chief things is the inability for C# to handle optional parameters, while VB.NET handles them with aplumb. COM and overloaded functions don’t mix very well.

One last thing I want you to be aware of before I go. If you rebuild the .NET solution in the download you will see a number of compiler warning regarding “Non COM Visible value types”. Don’t worry about them, they’re not showstoppers. I was just too lazy to code the necessary ComVisible attributes to get rid of them.

Here’s the Download and Remember, Have Fun

Download VFP XAML Solution and Example (approx. size 1.4MB)