Highlander: Convert HTML to Xaml Demo (.NET 3.0 / WPF)
Posted by: in Miscellaneous, tags: .net, CodeGear, DelphiThis is another demo that shows some features of the upcoming new Delphi version - codename “Highlander”. This time I show basically two things: 1: Working with Xaml, i.e. how to use WPF/.NET 3.0; 2: Mixed Pascal and C# project groups
1. Does Highlander do .NET 3.0?
If you closely watched the Highlander roadmap, then you might have noticed, that it only mentions .NET 2.0 support. So are we Delphi freaks again falling behind? Are we locked away from using any of the new .NET 3.0 stuff?
Nope! When you create a .NET application with Highlander, then it will create a .NET 2.0 one. But what does .NET 2.0 vs. 3.0 actually mean? .NET 3.0 is not a new .NET runtime version or so. It is “just” a set of new assemblies (assemblies for WPF for example). In other words a “.NET 3.0 WPF” application is still runniing on the .NET 2.0 core, it just references (and uses classes) of additional WPF assemblies.
Highlander being .NET 2.0 only, means just it does not have a wizard or so which adds the required assemblies for WPF (or other NET 3.0 features) - but of course we can add all required references manually:

In that screenshot you see the cor assemblies, which are needed to build WPF applications.
A HTML to XAML converter
In this demo we actually build a VCL.NET application, that will show a WPF Window, as response of a button click. In other words we are mixing VCL.NET with WPF. The purpose of this demo is to take any HTML and make a FlowDocument from it. FlowDocuments are part of the WPF world and hold content which gets reformatted depending on window size, resolution etc.
FlowDocuments are usually defined using XAML. The screen shot below shows a simple XAML representation of a FlowDocument and the HTML it was converted from.

Usually FlowDocuments won’t be created from HTML of course, but by using XAML authoring tools. To display FlowDocuments there are several classes in the new .NET 3.0 name spaces. Here we are going to use the FlowDocument reader. It can not be instantiated stand-alone, but it needs a container window. As you may spot from the screen shot below, the window has a Highlander icon, indicating that this window really has been created by Highlander.

What we see above is a WPF Window with just one element as content: a FlowDocumentReader instance. The code below shows how to create such a window from Delphi:
LWPFWindow := Window.Create; LWPFWindow.Width := 400; LWPFWindow.Height := 300; LFlowDocReader := FlowDocumentReader.Create; LWPFWindow.Content := FFlowDocReader;
(The same could be done using XAML)
This following part is the exiting one, it shows how VCL.NET code interacts with WPF classes. The content of a plain VCL TMemo (MemoXaml) is passed to the FlowDocumentReader instance:
LXamlReader := XamlReader.Create; LXamlStream:= MemoryStream.Create(Encoding.UTF8.GetBytes(MemoXaml.Lines.Text)); LFlowDocReader.Document := LXamlReader.Load(LXamlStream) as FlowDocument; LWPFWindow.Show;
2. Has C# been removed from Highlander?
If you look again at the roadmap and maybe also at Nick Hodge’s “Whats not in the roadmap” article, then you might come to the conclusion, “damn, they have crippled Highlander by removing C# support“. Well, yes, according the quoted articles, C# support has been reduced to “open, edit, syntax highlight, compile and debug” support only. I.e. no form designers etc.
But what does that mean? If you would want to do full C# based development, then you would go to Microsoft’s VisualStudio anyway. It just makes no sense to make Highlander’s C# personality a VisualStudio clone. We are Delphi developers anyway, aren’t we? Why would we want to do C# programming at all (in Delphi)?
There is a reason though, I want C# capabilities for in Delphi: When I find useful source code libraries, written in C#. I want to be able to put that C# code into my Delphi project group, and have Delphi compile it, so that I can directly reuse C# libraries in my Delphi application.
Thats exactly what Highlander allows me to do: I can create C# projects and have Delphi compile them, it also allows me to edit and debug the C# code - thats all I need!
The HtmlToXaml converter demonstrates that: The parser that analyzes the HTML and converts it to valid Xaml source, has been written in C# by Michael Weinhardt (Michael is working on WPF stuff at Microsoft). I created a C# library project in Highlander, added all of the original C# class files and got that compiled without any modification. The reason I’m not using a precompiled assembly, is that this converter is actually not finished yet. For example, it misses support for <img> tags which I might add later eventually …
In the Delphi project I just set that HTMLCoverter library as “Project reference” and all namespace/classes are available in my HTMLtoXAML Delphi project! The converter classes make heavy use of .NET 2.0 and 3.0 features, so its proven that Highlander does that do well, too!

In my opinion being able to reuse code (either as source or as precompiled assemblies) across languages is one of the, if not the, most powerful “fetaures” of the .NET world!
HtmlToXaml.zip (.NET 3.0 is required)

Entries (RSS)
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks