Delphi WPF Application Template Library
Have you heard about a new RAD Studio 2007 feature called "Template Libraries"? It is a new, very powerful Delphi IDE repository mechanism. It is no longer neccessary to write an Open Tools API package to add a custom project to RAD Studio 2007 "New Items" dialog. With "Template Libraries" the only thing you need to do is to create a special XML document with "bdstemplatelib" extension that describes a custom project and add it to the list of template libraries using "Template Libraries" dialog available from the "Tools->Template Libraries" menu option or from a context menu from within "New Items" dialog.
In the RAD Studio Help system you can find more information about template libraries. For your (and my;-) convinience I have copied and pasted below "Template Libraries" RAD Studio 2007 help topic:
Template LibrariesRAD Studio allows you to create multiple custom template libraries to use as the basis for creating future projects. Template libraries let you declare how a project can look, and enable you to add new types of projects to the New Items dialog box.Creating a template library is a two-step process.
- First, you create a RAD Studio project to use as the basis for the template, and an XML file with a .bdstemplatelib extension that describes the project. This project can be any kind of project that RAD Studio supports.
- Next, you add the project to the list of template libraries in the IDE by pointing to the .bdstemplatelib template library file in the Template Libraries dialog box, accessed with ToolsTemplate Libraries.
You can create your own template libraries, and you can use those created by other developers. RAD Studio delivers a default template library which cannot be removed, however you can add and remove custom template libraries.
Note: When creating a project to use with a template library, make sure the project is located in a subdirectory that contains no other projects. All of the files that are in the project should be located within the subdirectory or child subdirectories.
You can either create a single .bdstemplatelib template library file for each template library project, or list several related template projects in the same .bdstemplatelib template library file by assigning each project a separate unique item number.
"Creating Template Libraries"
From the list of preinstalled templates that come with a default installation of RAD Studio 2007 you might get an impression that "Template Libraries" are only used for so called "improved start-from-scratchability" (a term coined by Jim Tierney, Delphi R&D Engineer) for Delphi ASP.NET 2.0 MasterPage-based web applications, but in fact this mechanism can be used for other types of RAD Studio 2007 applications. According to help there are four types of projects that can be created with template libraries. The type of project is specified using one of a predefined values for "Creator" attribute inside the bdstemplatelib xml file:
- C++ Projects ("CBuilderProjectRepositoryCreator")
- Delphi Projects ("DelphiProjectRepositoryCreator")
- Delphi for .NET Projects ("DelphiDotNetProjectRepositoryCreator")
- Delphi for ASP .NET Projects ("AspDelphiProjectRepositoryCreator")
The Delphi for .NET compiler in RAD Studio 2007 supports .NET version 2.0. When Microsoft added support for WinFX technologies to .NET 2.0 they have decided to change .NET version number to 3.0 (probably to confuse everybody). So the bottom line is that Delphi for .NET 2007 currently supports .NET 2.0 and 3.0. During the Highlander (the codename for Delphi 2007) field test I’ve been looking into Delphi for .NET support for "Avalon" and "Indigo". These are codenames of respectively Windows Presentation Foundation (WPF) and Windows Communication Foundation (WCF) technologies. There is also another interesting .NET 3.0 technology called Windows Workflows, and I do not know what was a codename for a Windows Workflows.
With Template Libraries in place it should be really easy to add a minimal "Delphi WPF Application" to RAD Studio 2007 "New Items" dialog!
First I’m going to create a special folder called "template_libs" for my custom application template libraries under "RAD Studio" folder inside "My Documents". This seems to me as the most logical choice. The other two folders that already exists under "RAD Studio" are "Projects" where all new RAD Studio projects are stored by default and "code_templates" for custom RAD Studio Live Templates. Inside this folder I’m going to create a "WPFMinimal" subfolder for all files that make up a minimal Delphi for .NET Windows Presentation Foundation application.
Note: If you are on Windows XP you need to make sure that you have .NET 3.0 installed on your machine. On Vista .NET 3.0 is installed by default.
First we need to create a new Delphi for .NET Console Application.
The second step is to add to the project references to .NET assemblies that contain types we are going to use in code. These assemblies are located in two different location in a default .NET 3.0 installation: "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0" and "C:\WINDOWS\Microsoft.NET\Framework\v3.0\WPF"
At the minimum the WPF application needs to create an instance of "Application" class defined in the "System.Windows" namespace, and an instance of a class derived from a "Window" object. I have put a declaration of a "Window1" class derived from "Window" in a separate unit.
If you click on "Run" then a console window will be displayed and an empty WPF form. This is an "improved start-from-scratchability" for Delphi for .NET WPF application. Now it is your turn to define what goes into the "Window1" class declaration. Probably a constructor and destructor would be handy. Maybe a dock panel… For a good WPF background I would recommend Charles Petzold’s "Applications = Code + Markup" excellent book.
The last step is to create a *.bdstemplatelib file and add it to the IDE.
Template Libraries that come with RAD Studio 2007 installation are located in the default installation inside "C:\Program Files\CodeGear\RAD Studio\5.0\ObjRepos\DelphiDotNet" folder, with one subfolder per each new custom application. For example "AjaxMinimal" subfolder contains all files that make up a minimal Delphi ASP.NET AJAX application, and directly above the "AjaxMinimal" folder there is a corresponding "AjaxMinimal.bdstemplatelib" XML file. I’m going to modify this template to create a "WPFMinimal" application template.
<TemplateLibrary Version="1.0" id="WinFXminis">
<Name>WinFX Application Templates</Name>
<Description>Custom WinFX Application Templates</Description>
<Item id="item1" Creator="DelphiDotNetProjectRepositoryCreator">
<Description>Delphi for .NET Minimal WPF Application</Description>
Listing 1: WPFminimal.bdstemplatelib
To install new template library just open "Template Libraries" dialog from "Tools" menu and add "WPFMinimal.bdstemplatelib" to the list of installed templates.
If everything went fine you should be able to find a new item in "Delphi for .NET" project section in "New Items" dialog.
Just double-click on "WPF Application" item and you should have a new "WPFApplication" Delphi for .NET project open in the IDE. Click on "Run" and you should see an empty WPF window.
And what should I add to the "Window1" class?