Skip to content

UPF Binding Expressions

Robert Campbell edited this page Jun 3, 2023 · 1 revision

A binding expression is used in a UVML view definition to bind the value of a dependency property to some data source - generally speaking, either a property on the view model, or a property on another element within the same view.

Binding expressions can be identified by the fact that they are surrounded by {{double curly braces}}. Consider the following element definitions:

<Button Content="Foo"/>
<Button Content="{{Foo}}"/>

The first Button has its Content property set to the string "Foo", while the second Button has its default property bound to the value of the Foo property on the view model.

Additionally, you can use arrow notation to bind one element directly to another.

<HSlider Name="foo"/>
<NumericTextBlock>{{foo->Value}}</NumericTextBlock>

The NumericTextBlock element's default property is bound directly to the Value property on the HSlider named foo.

Finally, an expression which is bound to a string-typed property can specify how the string will be formatted by using the [] operator.

<TextBlock>{{DateTime.Now}}[hh:mm:ss]</TextBlock>

Compiling Binding Expressions

Binding expressions consist of C# code fragments, and must be compiled into an assembly before they can be used by an Ultraviolet application. This can be done by calling the CompileExpressionsIfSupported() method defined on the PresentationFoundation class:

var upf = Ultraviolet.GetUI().GetPresentationFoundation();
upf.CompileExpressionsIfSupported("Content", CompileExpressionsFlags.None);

Calling the method as in the example above will cause Ultraviolet to traverse the Content directory and all of its subfolders in search of UVML files containing binding expressions. These expressions will then be compiled into an assembly, called Ultraviolet.Presentation.CompiledExpressions.dll, which will be placed in the root directory of the application. This assembly can then be loaded by calling the LoadCompiledExpressions() method on PresentationFoundation.

Binding expression compilation is only supported on platforms which support dynamic code generation - at present, that means Windows, Linux, and OS X. For Android and iOS, you will need to compile the assembly on a desktop computer and distribute it with your application. Doing this is probably a good idea in general, at least for public releases of your application, as your application's startup time may be significantly lengthened by the compilation process.

Compilation Cache

When binding expressions are compiled, the compiler generates a cache file in addition to the expressions assembly. This file is called Ultraviolet.Presentation.CompiledExpressions.cache and like the expressions assembly it will be placed in the root folder of the application. It is a plain text file containing the names and hashes of every UVML file which was included in the compiled assembly. If the expressions compiler sees that nothing has changed since the cache was produced, it will skip the compilation process.

If you encounter any issues with the process of compiling expressions, it's probably a good idea to delete the cache file and try running the compilation process again.

Clone this wiki locally