WPF XAML demo

This page gives a Dyalog APL demonstratration of WPF XAMPL with some utility functions, delivered in the form of a namespace.

Please read the Dyalog tutorials for a more detailed explanation. Part of the code was taken from CodeProject: A Beginner's Guide.

What is XAML?
XAML stands for Extensible Application Markup Language (and pronounced "Zammel"). It's a simple language based on XML to create and initialize .NET objects with hierarchical relations.

All you can do in XAML can also be done in code. XAML is just another way to create and initialize objects.

You can use WPF without using XAML. It's up to you whether you want to declare it in XAML or write it as code. There is a XAML Overview from Microsoft.

FixSimpleXaml
If you install the attached namespace and execute the following 2 lines in your workspace:

is a function used to execute the XAML and return the root element as a .Net object. All the other elements that are named in the XAML will be attached to their names to the root object automatically.

For example, the element !TextBox that is named textBox1 (line 15) and the element Button that is named button1 (line 22) are attached automatically to the root element by the function :

That way you don't need to define a separate variable for each named element. If you install the user command called  you can see all the properties, methods and events of all the named objects by doing (click the combo of NOE to access all the named objects): In conclusion  is a simple function to use on simple XAML that does not have events and is properly formed. In production code you may want to do something like this:

FixXaml
and then if you click on the button, the value of the !TextBox will change. The value of the !TextBox can be retrieved simply by doing:

The function  is handling the event. The author has taken the convention of naming the callback functions with a double underscore prefix.

The goal is to be able to take the XAML directly from Visual Studio to APL. The single underscore '_' is a valid first character in Visual Studio and APL but is in conflict with the menu object that will accept an underscore as the first character to define a keyboard shortcut.

The line 5 of sample2 (x:Class="!WpfApplication3.!MainWindow") that is required by Visual Studio is removed by. See the comments in the function for more information.

In production code you may want to trap any error by using code like this:

About ⎕USING
In general, when using XAML there is no need to define a  before fixing it except when there is a 3rd party dll involved. For example, the variable  is defined as: and if you do: a Window will appear. In procedural code, the following is required for the same result: The first element of XAML must contain the xmlns= and the xmlns:x= declarations. This is instructing the parser (in our case System.Windows.Markup.!XamlReader in the function  and  ) to load a series of .NET namespaces required to parse the XAML. This is just a convention, there is actually no such web site.

3rd Party Dll
When using 3rd party dll, they must be added to the declaration in the first element of XAML. There are 2 choices on how to do it:

The method on the first line is recommended. Here is an example with the Syncfusion !PropertyGrid: and the XAML will look like this: but this is not enough,  must be set up correctly before fixing the XAML for 3rd party dlls in order for the parser to find the assembly. Here is an example for the Syncfusion !PropertyGrid (Syncfusion/4.5/ is the Dyalog sub-directory where the assemblies live): Another thing with the value of  for 3rd party dll is that it must be set in the __same namespace__ as where   or   is located (because   is Namespace scope). Alternatively, if you setup your  file that is in the same directory as the   file with a directive to look in the   directory you will not need to set up   and you don't need to worry about loading it into memory.

Typically the file will look like this:

When using procedural code instead of XAML you may want to define a one time  like this:

To add a new definition to an existing  and to prevent duplicate entries the 'Union' operator is used like this:

Fixing Images
In XAML you declare an Image object that is on disk the following way: When you want to keep the Image definition in the APL workspace (because it is easier that way to distribute the workspace or the namespace) one way of doing it is by keeping a Base64 definition of the Image. Base64 encoding is using a set of 64 visible characters to encode binary data.

It is widely used on the internet, for example in emails for images and binary attachments.

Here are the steps to use this technique with APL:

Step 1: Save all the images in the workspace with the function FileToBase64String
At design time, save the images in the workspace. The APL variable name of any image must be the original name of the image name in the XAML with the added suffix _b64 (naming convention only). The variable  in the attached namespace looks like this:

This format is perfect for scripted namespaces; compare this to storing the image with the original file values that would have some non-visual characters.

Step 2: Set the .Source of each Image with the function ImageFromBase64String
At run time, after obtaining the root object set the  of each Image from the previously saved APL variable. If there are many images in the XAML, the following lines of code can automate the process at run time: The icon and the cursor of the main window can be fixed manually by doing the following:

Routed Events
In WPF it is possible to set a single function that will receive all the Click events on the window (in this example it is ) by doing: This is useful because that way you don't need to define individual click events for each control.

When all this is done the window can be shown:

The User Command wpfXamlEditor is designed to edit the XAML saved in the workspace and on disk.

Inserting WPF Controls into Traditional Dyalog Windows
You can insert a WPF control into an already existing application developed with  by using an ElementHost and a Dyalog's. Here is an example of how to insert a WPF Button: Instead of a Button, you could use some complex XAML developed with Visual Studio. You just need to fix the XAML with  and make it the child of the   element.

How to Access the UI Thread from Another Thread
Since Net 2.0 Microsoft does not allow writing on the UI thread from another thread for security and stability reasons. Consequently, if you are executing a long calculation on another thread and you want to show the results by accessing directly the UI thread it is not possible.

With a Delegate
Here is an example of how to use that function:

The author has taken the prefix '⍝∇' instead of '⍝' because in production code you will probably want to erase all the comments in your runtime WS because they are useless and it is helping to obfuscate the code while taking less space.

With a DispatcherTimer
If you have a repetitive task to be executed on the UI thread then you can use a DispatcherTimer like this: Note that since the  is executed on the UI thread you cannot have a long callback because it will freeze the UI during its execution.

With a BackgroundWorker
The .NET framework provides a simple way to get started in threading with the BackgroundWorker component. This wraps much of the complexity and makes spawning a background thread relatively safe. It offers several features which include spawning a background thread, the ability to cancel the background process before it has completed, and the chance to report the progress back to your UI. The  is an excellent tool when you want multithreading in your application with access to the UI thread, mainly because it's so easy to use.

An example is included in this namespace. It is inspired by this article. You start the example by doing: A window will appear and if you click on the 'Start' button, the UI will be refreshed with the multiple of 42 between 1 and 1000 while simulating a long calculation. Check the comments of this function with the links for more details.

How to install wpfXamlDemo in your workspace

 * 1) Download wpfXamlDemo.v1.6.txt
 * 2) Do a Select all (Ctrl+A) and a copy (Ctrl+C).
 * 3) In your workspace execute
 * 4) Paste (Ctrl+V) the text into the Dyalog editor
 * 5) Press Escape and ')save' your workspace

Optionally to de-script the namespace you can do: