15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

744 ❘ ChaPTer 27 COre xAml<br />

Xaml-defined markup extensions<br />

Markup extensions provide a lot of capabilities. And indeed XAML-defined markup extensions have<br />

already been used in this chapter. x:Array, which was shown in the “Collections” section, is defined as the<br />

markup extension class ArrayExtension. With this markup extension, using the attribute syntax is not<br />

possible because it would be difficult to define a list of elements.<br />

Other markup extensions that are defined with XAML are the TypeExtension (x:Type), which returns the<br />

type based on string input; NullExtension (x:Null), which can be used to set values to null in XAML; <strong>and</strong><br />

StaticExtension (x:Static) to invoke static members of a class.<br />

WPF, WF, <strong>and</strong> WCF define markup extensions that are specific to these technologies. WPF uses markup<br />

extensions to access resources, for data binding, <strong>and</strong> color conversion; WF uses markup extensions with<br />

activities; <strong>and</strong> WCF defines markup extensions for endpoint definitions.<br />

reading <strong>and</strong> WriTing Xaml<br />

Several APIs exist for reading <strong>and</strong> writing XAML. There are high-level APIs that are easy to use but have<br />

less functionality <strong>and</strong> low-level APIs with more features. Technology-specific APIs that make use of specific<br />

WPF or WF features are also available. XAML can be read from a textual XML form, from BAML, or<br />

from object trees, <strong>and</strong> written to XML or object trees.<br />

Generic high-level APIs are available in the namespace System.Xaml. The class XamlServices allows<br />

loading, parsing, saving, <strong>and</strong> transforming XAML. XamlServices.Load() can load XAML code from a<br />

file, a stream, or a reader, or by using a XamlReader object. XamlReader (in the namespace System.Xaml)<br />

is an abstract base class that has several concrete implementations. XamlObjectReader reads an object<br />

tree, XamlXmlReader reads XAML from an XML file, Baml2006Reader reads the binary form of XAML.<br />

XamlDebuggerXmlReader from the namespace System.Activities.Debugger is a special reader for WF<br />

with special debugging support.<br />

When passing XAML code in a string to XamlServices, the Parse() method can be used. XamlServices.<br />

Save() can be used to save XAML code. With the Save() method you can use similar data sources as with<br />

the Load() method. The object passed can be saved to a string, a stream, a TextWriter, a XamlWriter,<br />

or an XmlWriter. XamlWriter is an abstract base class. Classes that derive from XamlWriter are<br />

XamlObjectWriter <strong>and</strong> XamlXmlWriter. In the release of .<strong>NET</strong> 4, there’s no writer for BAML code, but it<br />

is expected to have a BAML writer available at a later time.<br />

When converting XAML from one format in another, you can use XamlServices.Transform(). With the<br />

Transform method, you pass a XamlReader <strong>and</strong> a XamlWriter so that you can convert any format that is<br />

supported by specific readers <strong>and</strong> writers.<br />

Instead of using the high-level API XamlServices class, you can use generic low-level APIs directly, which<br />

means using specific XamlReader <strong>and</strong> XamlWriter classes. With a reader, you can read node by node from a<br />

XAML tree with the Read() method.<br />

The generic XamlServices class doesn’t support specific WPF features such as dependency properties or<br />

freezable objects. To read <strong>and</strong> write WPF XAML, you can use the classes XamlReader <strong>and</strong> XamlWriter<br />

from the namespace System.Windows.Markup that is defined in the assembly PresentationFramework<br />

<strong>and</strong> thus has access to the WPF features. The names of these classes might be confusing, as the same class<br />

names are used with classes from different namespaces. System.Xaml.XamlReader is the abstract base<br />

class for readers; System.Windows.Markup.XamlReader is the WPF class to read XAML. This can be even<br />

more confusing when using the Load method of the WPF XamlReader class that accepts a System.Xaml<br />

.XamlReader as argument.<br />

An optimized version to read XAML for WF is WorkflowXamlServices in the namespace<br />

System.Activities. This class is used to create dynamic activities during runtime.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!