270 likes | 657 Vues
WPF Unleashed XAML Demystified. By Prashanth Vijayaraghavan. XAML Defined. General purpose declarative programming language. XAML is the most concise way to represent user interfaces. Encourages separation of backend logic and front end appearance.
E N D
WPF Unleashed XAML Demystified. By Prashanth Vijayaraghavan
XAML Defined • General purpose declarative programming language. • XAML is the most concise way to represent user interfaces. • Encourages separation of backend logic and front end appearance. • .Net 3.0 includes compiler, run time parser for XAML.
Elements and Attributes • XAML maps .net namespaces, types, properties and events into XML namespaces, elements and attributes.
Elements and Attributes (Cont) • Object Element. • Property Attribute. • Event Attribute.
Elements and Attributes (Cont) • Standalone rendering VS Compilation mixing. • Order of property and event processing. A simple button which is rendered standalone
Namespaces • XAML code must contain at least one namespace xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation” • Additional namespaces can be defined by giving distinct prefix. xmlns:x=“http://schemas.microsoft.com.winfx/2006/xaml”
Property Elements Updating the WPF button with complex content
Property elements (Cont..) • Object elements - properties set using xml attributes. • Property elements - properties set with xml element. - They can never have attributes eg:<Button.Content>ok</Button.Content>
Type Converters • Type converters convert string representation to the desired data type. Eg:
Type Converters (Cont..) • WPF provides type converters for many common data types like brush, color, fontweight, point etc. • Can create our own type converter by deriving from TypeConverter. • Type converter supports case insensitive strings.
Markup Extensions • Like type converters, markup extensions evaluate a string value at run time and produce appropriate objects. • Attribute value enclosed in curly braces({}).
Markup Extensions (Cont ..) • Escaping curly braces
Children of object elements • All Xaml files must have single root object element which can contain hierarchy of child object elements. • Three types of children - A Value for content property. - Collection items. - A value that can be type converted to parent.
Collection Items Items property to insert individual items
Dictionaries • System.Windows.ResourceDictionary is a commonly used collection type. • It supports adding of key value pair to any collection implementing Idictionary.
Types in .NET FCL Integration with types from .net framework class library(int32)
More Type Conversions Brush is an abstract base class for concrete classes such as SolidColorBrush, GradientBrush etc. Is equivalent to
Loading and Parsing XAML at run time • xmlReader.load method parse XAML, create appropriate .net object and return an instance of root element.
Naming XAML elements Name keyword for giving any element a name FindName method searches its children and returns the desired instance.
Compiling XAML XAML compilation involves three things. • Converting a XAML file into a special binary format. • Embedding the converted content as a binary resource in the assembly being built. • Connecting XAML and procedural code.
Compilation: Mixing XAML with procedural code Defining sub class in the root element Code behind file with the same class name given above
Files generated when compiling xaml In the case of previous example • A Baml file (MyWindow.baml)- Embedded in the assemble as a binary resource. • A c# source file(MyWindow.g.cs)- Gets Compiled into the assembly.
BAML- Binary application markup language • XAML that has been parsed, tokenized and converted into binary form. • XAML to BAML compilation does not include generation of procedural source code. • Faster to load and parse.
Procedural code inside XAML • Contents of the code are put inside X:Code element