WPF journey (II)-XAML, wpf journey xaml

Source: Internet
Author: User

WPF journey (II)-XAML, wpf journey xaml

What is XAML?

XAML (short for Extensible Application Markup Lanaguage, pronounced "zammel") is the Markup language used to instantiate. NET objects. Although XAML is a technology that can be used in many different problem fields, it mainly serves to construct the WPF user interface. In other words, the XAML document defines the panel, buttons, and layout of various controls that make up windows in the WPF application.

 

WPFProblems with the previous graphic user Design Interface

1. Each Graphic Element (such as the background and button) needs to be exported as a separate bitmap. This limits the ability to combine bitmap and use dynamic effects, such as anti-aliasing, transparency, and shadow effects.

2. A lot of user interface logic needs to be embedded into the code by developers, and graphic designers cannot control any details.

3. There is no inherent connection between different graphic elements, and mismatched image sets are often used.

4. When the image size is adjusted, the quality will inevitably be lost. The bitmap-based interface depends on the resolution, which means it cannot adapt to large displays and high-resolution display settings.

 

XAMLVariant

We use XAML to represent the entire XAML language. It is a language specifically used to represent a. NET object tree based on the general XML syntax. It includes the following sets:

1. wpf xaml contains content elements that describe WPF.

2. xps xaml is a part of wpf xaml. It defines an XML Representation for formatted electronic documents.

3. Silverlight XML is a subset of wpf xaml for Microsoft Silverlight applications.

4. wf xaml contains elements that describe the content of WF (Work FLOW, workflow.

 

XAMLCompile

WPF uses BAML (Binary Application Markup Lanaguage, Binary Application Markup Language) to overcome this disadvantage. BAML is not a new thing. It is actually a binary representation of XAML. All XAML is converted to BAML, and these BAML are then embedded into the final DLL or EXE assembly as resources. Most programmers do not need to consider the conversion from XAML to BAML, because the compiler will execute this job in the background. However, you can also use uncompiled XAML, which may make sense for situations where you need to provide real-time user interfaces (for example, extracting content from a database as a piece of XAML tag ).

 

XAMLStandard

1. Each element in the XAML document is mapped to an instance of the. NET class, and the element name completely corresponds to the class name.

2. Like all XML documents, another element can be nested in one element.

3. Attribute can be used to set the attributes of each class. In some cases, the feature is insufficient to use nested tags through special syntax ).

 

XAML namespace

The xmlns feature is a special feature in XML. It is used to declare the namespace. These two namespaces will appear in all the created wpf xaml.

1. xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" is the core namespace of WPF. It contains all WPF classes, including controls used to build user interfaces.

2. xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" is the XAML namespace. It includes a variety of XAML practical features that affect the interpretation of the document. The namespace is mapped to the prefix x. This means that the namespace can be used by placing the namespace prefix x before the element name (for example, <x: ElementName> ),

"X"Prefix/XAMLLanguage XAMLCommon namespace structures:

X: key: Set a unique user-defined key for each resource in the XAML ResourceDictionary.

X: class: provides a code hiding class for the XAML page to specify the code namespace and code name. For example, <window x: Class = "WindowApplication1.Window1">.

X: Name: After processing the object elements defined in XAML, specify a runtime object Name for the objects in the runtime code. You can set x: Name in XAML as a variable declared in code. Example: <Grid x: Name = "grid1">

X: Uid: identifies certain elements. The localized resources should be used with some attribute values of the element. Such as resource files:

 

Associate a control with a resource file: <TextBlock x: Uid = "Greeting" Text = ""/>


XAMLAttributes and events in

1. Type Converter: To associate string values with non-string attributes, The XAML parser needs to perform conversion. The conversion is performed by the type converter, which provides the method used to convert a specific.. NET data type to any other. NET type or any other.. NET type to the specified data type. XAML uses two steps to find the type converter:

A. Check the attribute declaration to find the TypeConverter feature.

B. If the attribute declaration does not contain TypeConverter, The XAML parser checks the class declaration of the corresponding data type.

Note: Like all XML-based languages, XAML is case sensitive, which means <button> cannot be used instead of <Button>. However, Type converters are usually case insensitive, this means that ForeGround = "White" and ForeGround = "white" have the same effect.

 

2. Complex attributes

Although Type converters are easy to use, they cannot solve all the actual problems. Fortunately, XAML provides another option: attribute element syntax (Property-element-syntax). You can use the attribute element syntax to add child elements named as Parent. PropertyName. For example:

<Grid>

<Grid. Background>

Blue

</Grid. Background>

</Grid>

What really works is the period (.) In the element name (.). This period separates this attribute from other types of nested content. This period separates this attribute from other types of nested content. Next let's look at the nested attributes:

<Grid>

<Grid. Background>

<LinearGradientBrush>

<LinearGradientBrush. GradientStops>

<GradientStop Offset = "0.00" Color = "Red"> </GradientStop>

<GradientStop Offset = "0.50" Color = "Indigo"> </GradientStop>

<GradientStop Offset = "1.00" Color = "Violet"> </GradientStop>

</LinearGradientBrush. GradientStops>

</LinearGradientBrush>

</Grid. Background>

</Grid>

Any XAML tag set combination can be replaced by a series of code statements that execute the same task. The labels shown above are equivalent to the following code:

Var brush = new LinearGradientBrush ();

Var gradientStop1 = new GradientStop ();

GradientStop1.Offset = 0;

GradientStop1.Color = Colors. Red;

Brush. GradientStops. Add (gradientStop1 );

 

Var gradientStop2 = new GradientStop ();

GradientStop2.Offset = 0.5;

GradientStop2.Color = Colors. Indigo;

Brush. GradientStops. Add (gradientStop2 );

 

Var gradientStop3 = new GradientStop ();

GradientStop3.Offset = 1;

GradientStop3.Color = Colors. Violet;

Brush. GradientStops. Add (gradientStop3 );

 

Grid1.Background = brush;

 

3.Tag Extension

The markup extension allows access to the XAML file to only declare the value or behavior of the element based on the supported types. For most attributes, The XAML attribute syntax can work very well. However, in some cases, the attribute value cannot be hard-coded. For example, you may want to set the attribute value to an existing object, or you may want to dynamically set the attribute value by binding one property to another control, in these cases, you need to use tag extension-a special syntax for setting properties in unconventional ways.

Tag extensions can be used in nested tags or XML features. When used for features, they are always surrounded by curly braces. For example:

<Button Foreground = "{x: Static SystemColors. ActiveCaptionBrush}"> </Button>

Mark extended usage{Tag extension classParameter}Syntax. In the above example, the markup Extension is the StaticExtension class (according to the Conventions, the last word Extension can be omitted when referencing the Extension class ). The x prefix indicates finding the StaticExtension class in the XAML namespace. The final result of the above XAML is the same as that of the following code:

CmdBtn. Foreground = SystemColors. ActiveCaptionBrush;

Because the tag extension ing is a class, they can also be used as nested attributes, as shown below:

<Button>

<Button. Foreground>

<X: Static Member = "SystemColors. ActiveCaptionBrush"> </x: Static>

</Button. Foreground>

</Button>

 

4.Additional attributes

Additional attributes (Attached property) Is a programming concept introduced in XAML. In this way, attributes can be owned and defined for special types, but attributes can be set as properties or attribute elements on any element. Its working principle is that each control has a inherent property (for example, does the text box have its specific font and text color, which are specified through the FontFamily and ForeGround attributes ). When a control is placed in a container, additional features are obtained based on the container type control.

The additional attributes are named in the form of two parts: Definition type. attribute name. For example, you can place controls on each row of the grid by attaching properties:

<TextBox Grid. Row = "0"> Some words </TextBox>

<TextBox Grid. Row = "1"> Some words </TextBox>

The additional attributes are not actually called. They are actually converted to method calls. The XAML parser calls the static method in the following form: DefiningType. SetPropertyName (). For example, in the above XAML code, the definition type is Grid class and the attribute is Row, so the parser calls Grid. setRow () method, specifically: Grid. setRow (txtAns, 0 ).

 

5.Nested Element

XAML documents are arranged into a huge nested element tree. XAML allows each element to decide how to process nested elements. One of the following three mechanisms is used for interaction, the order of value is also the order of the three mechanisms listed below:

A. if the parent element implements the IList interface, the parser will call the IList. Add () method and pass the method into the child element as the parameter.

B. If the parent element implements the IDictionary interface, the parser calls the IDictionary. Add () method and passes the child element as a parameter for the method. When using a dictionary set, you must also set the x: Key feature to specify a Key name for each entry.

C. If the parent element uses the ContentProperty attribute for modification, the parser uses the child element to set the corresponding attribute.

 

6.Special characters and white spaces

XAML is restricted by XML rules. If we try to use some special characters, such as &, <and>, it will be troublesome because the XAML parser thinks you are processing other things. Sometimes this is not the expected result. In these cases, you need to use a specific conversion for the element, such as & ltl substitution <, & gt; substitution>. When you want the text to contain a series of spaces, the xml: space = "preserve" feature must be used for elements.

 

XAMLEvent

The feature can also be used to associate Event Handlers. Syntax: event name = "event handler method name", for example:

<Button... Click = "cmdBtn_Click"> </Button>

The following is a and required method:

Private void cmdBtn_Click (object sender, RoutedEventArgs e)

{

// Do something else

}

 

When you add the event handler feature, VS can only sense and provide great help. VS will display a drop-down list containing all the appropriate event handlers in the Code hiding class, you can select and create this event handler.

 

I recently started to learn about WPF and DevExpress to improve my C/S architecture programming skills. I will have some experiences in the learning process, so I will write some blogs to record these ideas. If you are interested, you can share them with me. So let's start the journey between WPF and DevExpress!

QQ group: 32745894. Welcome to join the discussion!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.