WP7 XAML Basics

Source: Internet
Author: User

XAML is the abbreviation of Extensible Application Markup Language (eXtensible Application Markup Language. It is a simple XML-based language used to create. Net objects and their relationships. Although it was initially designed for WPF to create an object tree. However, currently, XAML can be used not only to create WPF and sliverlight user interfaces, but also to declare windows workflow (WF) processes or standard XPS electronic files.

All classes in WPF have a no-argument constructor and flexible attribute application. The purpose is to fully adapt to the XML language, such as in XAML.

Advantages of XAML:

The first point is that the code that can be used in XAML can also be implemented. XAML is just another way to create and initialize objects. You do not need to use XAML to implement a WPF application. Coding in XAML depends entirely on your own ideas. However, declaring the user interface in XAML has the following benefits:

  • The XAML code is simple, clear, and easy to read.
  • Separated logic code and design code
  • Graphic design tools such as expression blend all create XAML files
  • The separation of logic and design in XAML also represents the independence of the designer and developer roles.
XAML and code:

Use a simple text box and button added to stackpanel to compare the XAML and C # code.

<StackPanel>    <TextBlock Margin="20">Welcome to the World of XAML</TextBlock>    <Button Margin="10" HorizontalAlignment="Right">OK</Button></StackPanel> 

The following shows the same effect in C # code:

// Create the StackPanelStackPanel stackPanel = new StackPanel();this.Content = stackPanel; // Create the TextBlockTextBlock textBlock = new TextBlock();textBlock.Margin = new Thickness(10);textBlock.Text = "Welcome to the World of XAML";stackPanel.Children.Add(textBlock); // Create the ButtonButton button = new Button();button.Margin= new Thickness(20);button.Content = "OK";stackPanel.Children.Add(button);

You can clearly see that the XAML version is concise and easy to read. This is the strength of the XAML expression.

Elements are attributes:

From XML statement<Button content = "OK"/>. However, what should we do if we want to place a more complex object in attributes such as content, such as an image, or even itself or a whole grid panel. We can use the element as the attribute syntax. It allows us to extract attributes as its own child elements.

<Button>  <Button.Content>     <Image Source="Images/OK.png" Width="50" Height="50" />  </Button.Content></Button> 

Implicit type conversion:

An important concept in WPF is implicit type conversion, which is processed in the background. The following defines a borderbrush. The word "blue" is just a string. But the implicit brushconverter will convert it intoSystem.Windows.Media.Brushes.Blue. Similarly, the value of thickness is implicitly convertedThickness object. WPF contains many such built-in types and supports custom type conversion.

<Border BorderBrush="Blue" BorderThickness="0,10"></Border> 

 

ExtensionTag:

Extended labels are dynamic placeholders of attribute values in XAML. It solves the problem of attribute assignment during runtime. Extension labels are usually contained in a set of braces, such as background = "{staticresource normalbackgroundbrush }". WPF has some built-in extension labels, and you can derive custom extensions from markupextension. These built-in labels include:

  • Binging ---Bind the values of two attributes together
  • Staticresource ---One-time resource matching
  • Dynamicresource ---Dynamically match resource items
  • Templatebinding ---Bind the template control of the property to the control on which the property depends
  • X: static ---Set the static Attribute Value
  • X: NULL ---Null is returned)

The first tag in braces is the name of the extension tag. AllProperty = ValueFormat. The following example shows a label, whose content is bound to the text (text) of a text box (testbox ). When you enter a value in the text box, its text attributes will change, and the bound extension label will automatically change the label content.

<TextBox x:Name="textBox"/><Label Content="{Binding Text, ElementName=textBox}"/>

Namespace:

The beginning of each XAML must contain two namespaces. The first one is:http://schemas.microsoft.com/winfx/2006/xaml/presentation.It maps all the WPF controls under system. Windows. controls.

The second one isHttp://schemas.microsoft.com/winfx/2006/xaml,System.Windows.MarkupThe specified XAML keyword.

The ing between the XML namespace and the CLR namespace is determined by the Assembly levelThe xmlnsdefinition attribute is complete. You can also use CLR-namespace to directly reference a clr namespace in XAML. For example:

<Window xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”></Window>

Original translated from: http://www.cnblogs.com/jimson/archive/2010/07/20/XAML.html

XAML is the abbreviation of Extensible Application Markup Language (eXtensible Application Markup Language. It is a simple XML-based language used to create. Net objects and their relationships. Although it was initially designed for WPF to create an object tree. However, currently, XAML can be used not only to create WPF and sliverlight user interfaces, but also to declare windows workflow (WF) processes or standard XPS electronic files.

All classes in WPF have a no-argument constructor and flexible attribute application. The purpose is to fully adapt to the XML language, such as in XAML.

Advantages of XAML:

The first point is that the code that can be used in XAML can also be implemented. XAML is just another way to create and initialize objects. You do not need to use XAML to implement a WPF application. Coding in XAML depends entirely on your own ideas. However, declaring the user interface in XAML has the following benefits:

  • The XAML code is simple, clear, and easy to read.
  • Separated logic code and design code
  • Graphic design tools such as expression blend all create XAML files
  • The separation of logic and design in XAML also represents the independence of the designer and developer roles.
XAML and code:

Use a simple text box and button added to stackpanel to compare the XAML and C # code.

<StackPanel>    <TextBlock Margin="20">Welcome to the World of XAML</TextBlock>    <Button Margin="10" HorizontalAlignment="Right">OK</Button></StackPanel> 

The following shows the same effect in C # code:

// Create the StackPanelStackPanel stackPanel = new StackPanel();this.Content = stackPanel; // Create the TextBlockTextBlock textBlock = new TextBlock();textBlock.Margin = new Thickness(10);textBlock.Text = "Welcome to the World of XAML";stackPanel.Children.Add(textBlock); // Create the ButtonButton button = new Button();button.Margin= new Thickness(20);button.Content = "OK";stackPanel.Children.Add(button);

You can clearly see that the XAML version is concise and easy to read. This is the strength of the XAML expression.

Elements are attributes:

From XML statement<Button content = "OK"/>. However, what should we do if we want to place a more complex object in attributes such as content, such as an image, or even itself or a whole grid panel. We can use the element as the attribute syntax. It allows us to extract attributes as its own child elements.

<Button>  <Button.Content>     <Image Source="Images/OK.png" Width="50" Height="50" />  </Button.Content></Button> 

Implicit type conversion:

An important concept in WPF is implicit type conversion, which is processed in the background. The following defines a borderbrush. The word "blue" is just a string. But the implicit brushconverter will convert it intoSystem.Windows.Media.Brushes.Blue. Similarly, the value of thickness is implicitly convertedThickness object. WPF contains many such built-in types and supports custom type conversion.

<Border BorderBrush="Blue" BorderThickness="0,10"></Border> 

 

ExtensionTag:

Extended labels are dynamic placeholders of attribute values in XAML. It solves the problem of attribute assignment during runtime. Extension labels are usually contained in a set of braces, such as background = "{staticresource normalbackgroundbrush }". WPF has some built-in extension labels, and you can derive custom extensions from markupextension. These built-in labels include:

  • Binging ---Bind the values of two attributes together
  • Staticresource ---One-time resource matching
  • Dynamicresource ---Dynamically match resource items
  • Templatebinding ---Bind the template control of the property to the control on which the property depends
  • X: static ---Set the static Attribute Value
  • X: NULL ---Null is returned)

The first tag in braces is the name of the extension tag. AllProperty = ValueFormat. The following example shows a label, whose content is bound to the text (text) of a text box (testbox ). When you enter a value in the text box, its text attributes will change, and the bound extension label will automatically change the label content.

<TextBox x:Name="textBox"/><Label Content="{Binding Text, ElementName=textBox}"/>

Namespace:

The beginning of each XAML must contain two namespaces. The first one is:http://schemas.microsoft.com/winfx/2006/xaml/presentation.It maps all the WPF controls under system. Windows. controls.

The second one isHttp://schemas.microsoft.com/winfx/2006/xaml,System.Windows.MarkupThe specified XAML keyword.

The ing between the XML namespace and the CLR namespace is determined by the Assembly levelThe xmlnsdefinition attribute is complete. You can also use CLR-namespace to directly reference a clr namespace in XAML. For example:

<Window xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”></Window>

Original translated from: http://www.cnblogs.com/jimson/archive/2010/07/20/XAML.html

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.