Download presentation
Presentation is loading. Please wait.
Published byCrystal Booth Modified over 9 years ago
1
Rujchai Ung-arunyawee Department of Computer Engineering Khon Kaen University
2
What is XAML short for Extensible Application Markup Language, and pronounced “zammel”) Markup (declaraive) language used to instantiate.NET objects XAML, like all XML-based languages, is case-sensitive. That means you can’t substitute for can be applied to many different problem domains Visual Studio or Microsoft Expression Blend can be used to generate the XAML
3
Advantages of XAML XAML code is short and clear to read Separation of designer code and logic Graphical design tools like Expression Blend require XAML as source. The separation of XAML and UI logic allows it to clearly separate the roles of designer and developer
4
XAML vs. Code XAML version much shorter and clearer to read
5
The Variants of XAML WPF XAML encompasses the elements that describe WPF content, such as vectorgraphics, controls, and documents. XPS XAML (XML Paper Specification) is the part of WPF XAML that defines an XML representation for formatted electronic documents. Silverlight XAML is a subset of WPF XAML that’s intended for browser applications. WF XAML encompasses the elements that describe Windows Workflow Foundation (WF) content.
6
XAML Compilation When you compile a WPF application in Visual Studio, all your XAML files are converted into BAML and that BAML is then embedded as a resource into the final DLL or EXE assembly
7
XAML Basics Every element in a XAML document maps to an instance of a.NET class. The name ofthe element matches the name of the class exactly. For example, the element instructs WPF to create a Button object. As with any XML document, you can nest one element inside another. However, nesting is usually a way to express containment You can set the properties of each class through attributes. However, in some situations an attribute isn’t powerful enough to handle the job. In these cases, you’ll use nested tags with a special syntax.
8
XAML Basics (cont.) The top-level Window element, which represents the entire window The Grid, in which you can place all your controls For the Window element you’ll find several interesting attributes, including a class name and two XML namespaces Also find the three properties which tells WPF to create a window with the caption Window1 and to make it 300 by 300 units large
9
XAML Namespaces To figure out which class you really want, the XAML parser examines the XML namespace that’s applied to the element. The xmlns attribute is a specialized attribute in the world of XML that’s reserved for declaring namespaces
10
XAML Namespaces (cont.) In the sample document shown earlier, two namespaces are defined: http://schemas.microsoft.com/winfx/2006/xaml/presentation is the core WPF namespace this namespace is declared without a namespace prefix, so every elementis automatically placed in this namespace http://schemas.microsoft.com/winfx/2006/xaml is the XAML namespace. This namespace is mapped to the prefix x. That means you can apply it byplacing the namespace prefix before the element name (as in ).
11
Using Other Namespaces You need to map the.NET namespace to an XML namespace. XAML has a special syntax for doing this Prefix is the XML prefix you want to use to indicate that namespace in your XAML markup. For example, the XAML language uses the x: prefix. Namespace is the fully qualified.NET namespace name. AssemblyName is the assembly where the type is declared, without the.dll extension. This assembly must be referenced in your project.
12
Other Namespaces: Examples Here’s how you would gain access to the basic types in the System namespace and map them to the prefix sys: Here’s how you would gain access to the types you’ve declared in the MyProject namespace of the current project and map them to the prefix local: Now, to create an instance of a class in one of these namespaces, you use the namespace prefix:
13
Other Namespaces: Tips You can use any namespace prefix you want. The sys and local prefixes are commonly used when importing the System namespace and the namespace for the current project Basically, XAML doesn’t support parameterized constructors, and all the elements in WPF elements include a no-argument constructor If the class you want to use doesn’t have a no-argument constructor, you can supply the string representation of your data as content inside your tag
14
The Code-Behind Class using the Class attribute to make a functioning application a way to connect the event handlers that contain your application code The x namespace prefix places the Class attribute in the XAML namespace the Class attribute tells the XAML parser to generate a new class which derives from the class that’snamed by the XML element. this example creates a new class named Window1, which derives from the base Window class.
15
Visual Studio helps you out by automatically creating a partial class where you can place your event handling code.
16
Naming Elements In order to read or change properties or attach and detach event handlers on the fly, the element must include a XAML Name attribute. You can make this change by hand in the XAML document, or you can select the grid in the Visual Studio designer and set the Name property using the Properties window. Now you can interact with the grid in your Window1 class code by using the name grid1:
17
Properties and Events in XAML The eight ball window includes four controls
18
Simple Properties and Type Converters the attributes of an element set the properties of the corresponding object. For example, the text boxes in the eight ball example configure the alignment, margin, and font: XAML parser needs to perform a conversion between string values and non string properties by type converters type converters usually aren’t case-sensitive, which means both Foreground="White" and Foreground="white" have the same result.
19
Complex Properties XAML provides another option: property-element syntax. The key detail that makes this work is the period (.) in the element name.
21
Markup Extensions sets a property value in a nonstandard way to an object that already exists to a property in another control by binding it dynamically can be used in nested tags or in XML attributes When they’re used in attributes, they are always bracketed by curly braces {}. the syntax: {MarkupExtensionClass Argument}.
22
Markup Extensions (cont.) For example In this case, the markup extension is the StaticExtension class. By convention, you can drop the final word Extension when referring to an extension class. The x: prefix indicates that the StaticExtension is found in one of the XAML namespaces. markup extensions that are a part of the WPF namespaces, don’t have the x: prefix
23
Markup Extensions (cont.) Because markup extensions map to classes, they can also be used as nested properties Similar to
24
Attached Properties Properties that may apply to several controls but are defined in a different class In WPF, attached properties are frequently used to control layout. Two-part name syntax: DefiningType.PropertyName
25
Nesting Elements XAML documents are arranged as a heavily nested tree of elements Nested elements are evaluated in this order If the parent implements IList, the parser calls IList.Add() and passes in the child If the parent implements IDictionary, the parser calls IDictionary.Add() and passes in the child. When using a dictionary collection, you must also set the x:Key attribute to give a key name to each item. If the parent is decorated with the ContentProperty attribute, the parser uses the child to set that property.
26
Nesting Elements (cont.) each GradientStop should be added to the collection using the IList.Add() method:
27
Nesting Elements (cont.) Some properties might support more than one type of collection. In this case, you need to add a tag that specifies the collection class
28
Special Characters By the rules of XML, XAML pays special attention to a few specific characters, such as & and.
29
Special Characters (cont.) When the XAML parser reads this, it correctly understands that you want to add the text and it passes a string with this content, complete with angled brackets, to the Button.Content property. This limitation is a XAML detail and it won’t affect you if you want to set the Button.Content property in code
30
Whitespace Use the xml:space="preserve” attribute on your element to include a series of several spaces The xml:space attribute is a part of the XML standard Once you switch it on, all the whitespace inside that element is retained In this example, the text in the text box will include the hard return and tab that appearbefore the actual text. It will also include the series of spaces inside the text and the hard return that follows the text.
31
Events Attributes can also be used to attach event handlers Syntax: EventName="EventHandlerMethodName“ For example, the Button control provides a Click event. You can attach an event handler like this: This assumes that there is a method with the name cmdAnswer_Click in the code-behind class.
32
Events (cont.) The event handler must have the correct signature
33
Events (cont.) The event model inWPF is different than in earlier versions of.NET It supports a new model that relies on event routing
34
Events (cont.) Visual studio with IntelliSense helps you add an event handler attribute.
35
The Last Word You’ve learn XAML syntax. You’ve considered key XAML ingredients, such as type converters, markup extensions, and attached properties. You’ve learned how to wire up a code-behind class that can handle the events raised by your controls.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.