[. NET] XAML (1) -- Object generation

Source: Internet
Author: User


Preface
XAML is a declarative markup language launched by Microsoft. It uses XML format to allow developers to design application programming interfaces. In Microsoft's recent development platforms, such as WPF, Silverlight, WP7, and even Win8 Metro style app development, you can see the presence of XAML. The magical cross-platform operation of XAML is that XAML is not involved in the operation and mechanism of the execution platform... and so on. Just follow the developer's design and create corresponding objects for the execution platform to use. For example:
 
XAML example
<Phone: PhoneApplicationPage
X: Class = "XamlSample. MainPage"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: phone = "clr-namespace: Microsoft. Phone. Controls; assembly = Microsoft. Phone"
Xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Mc: Ignorable = "d" d: DesignWidth = "480" d: DesignHeight = "800">
 
<TextBlock x: Name = "ShowTextBlock" Text = "Hello World" FontSize = "72"/>
 
</Phone: PhoneApplicationPage>
Namespace XamlSample
{
Public partial class MainPage: PhoneApplicationPage
{
Public MainPage ()
{
// Base
InitializeComponent ();
}
}
}
 
Code example
<Phone: PhoneApplicationPage
X: Class = "XamlSample. MainPage"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: phone = "clr-namespace: Microsoft. Phone. Controls; assembly = Microsoft. Phone"
Xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Mc: Ignorable = "d" d: DesignWidth = "480" d: DesignHeight = "800">
 
</Phone: PhoneApplicationPage>
Namespace XamlSample
{
Public partial class MainPage: PhoneApplicationPage
{
Public MainPage ()
{
// Base
InitializeComponent ();
 
// Create
TextBlock showTextBlock = new TextBlock ();
ShowTextBlock. Name = "ShowTextBlock ";
ShowTextBlock. Text = "Hello World ";
ShowTextBlock. FontSize = 72;
This. Content = showTextBlock;
}
}
}
 
Execution result



The execution results of the two WP7 sample programs are displayed on the screen as Hello World. We add breakpoints to the program code to view the objects (such as) in the execution results. We can see that the structure of the objects produced by the two examples is the same. That is to say, whether it is using XAML or program code to construct a screen object, it is the same .. NET creates an object according to The XAML content designed by the developer, just as the developer creates an object using program code. After understanding this example, you can simply say that XAML is the configuration file used to generate objects, and the execution platform uses the objects generated by XAML. While. NET uses Reflection to generate objects according to settings, we can also broadly say that "XAML is the Reflection configuration file used to generate objects 」.

 

XAML sample interruption

 

 

Code sample interruption

 


This article uses the "XAML is the Reflection configuration file used to generate objects" to analyze the XAML. To help developers understand XAML and know how to generate objects through XAML.
 
Object-Element
The following section of XAML represents a TextBlock object. When the program is executed,. NET analyzes the XAML Element to generate a TextBlock object. In this way, the XAML Element of an Object is called "Object-Element 」.
 
<Sample: TextBlock x: Name = "ShowTextBlock" Text = "Hello World" FontSize = "72" xmlns: sample = "clr-namespace: System. windows. controls; assembly = System. windows "/>
 
Developers who have used Reflection will know the component name, namespace, and category name. These three string data can be reflected to generate an object. In Object-Element, the three data types have their own specifications. Interpret the above Object Element according To The XAML specification. The component name is System. windows and namespace are System. windows. controls and category name are TextBlock .. After the Object Element is parsed, A TextBlock Object is generated based on the reflection of the string data. Replacing the TextBlock in the Hello World example with this XAML Element can still display Hello World normally.
 
<Phone: PhoneApplicationPage
X: Class = "XamlSample. MainPage"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: phone = "clr-namespace: Microsoft. Phone. Controls; assembly = Microsoft. Phone"
Xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Mc: Ignorable = "d" d: DesignWidth = "480" d: DesignHeight = "800">
 
<Sample: TextBlock x: Name = "ShowTextBlock" Text = "Hello World" FontSize = "72" xmlns: sample = "clr-namespace: System. windows. controls; assembly = System. windows "/>

</Phone: PhoneApplicationPage>
 
Of course, this kind of XAML content seems to be different from the common XAML content, which is much more complicated. Because XAML is developed from XML, many formats follow the XML specification. Many namespaces can be provided by the upper-layer elements, which greatly reduces the data content that needs to be set in XAML. Developers interested in this section can refer to the relevant XML technical materials. In addition, various development platforms also define some default keywords to make the XAML design more concise. For this part of interested developers, refer to the relevant technical materials of the development platform. In the preceding example, the TextBlock preset by the development platform is generated, and the namespace preset by the Development Platform has been announced on PhoneApplicationPage, therefore, you can omit the component name and namespace. After the simplification of these specifications, The XAML can produce common XAML data content.
 
Property-Attribute
The following section of XAML represents a TextBlock object, which has a Text attribute. When the program is executed,. NET analyzes the XAML Element to generate a TextBlock object, and sets the Text attribute of the TextBlock object to Hello World and FontSize to 72. In this way, an object Attribute is set, which is called "Property-Attribute 」.
 
<TextBlock x: Name = "ShowTextBlock" Text = "Hello World" FontSize = "72"/>
 
By querying MSDN, you can find that the FontSize attribute of TextBlock is a property of the type System. Double. Because XAML is in XML format, it is restricted to only data in string format. Due to this restriction, when. NET analyzes the Property-Attribute, it will try to convert the string data to the type of the object Property. The following example shows a. NET Error Notification during execution, informing you that the string data cannot be converted to System. Double.
 
<TextBlock x: Name = "ShowTextBlock" Text = "Hello World" FontSize = "72 Clark"/>

 

 

Property-Element
In the XAML specification, the TextBlock example in the Property-Attribute section can also be written as the format of the following example. Rewrite the FontSize attribute of TextBlock to a pair of independent labels and write the set values in the label content. Setting an object Property like this is called "Property-Element 」.
 
<TextBlock x: Name = "ShowTextBlock" Text = "Hello World">
<TextBlock. FontSize>
72
</TextBlock. FontSize>
</TextBlock>
 
The writing of Property-Element seems redundant, but it is actually designed for the extendibility of XAML. Generally, some attributes of an object are not simply int or double type, but may also be an object (Class) or a structure (Struct ). An object has attributes, and the entire object is grown in a tree structure. In this case, the Property-Attribute uses a string to set the tree structure of this object. Property-Element is defined. You can use Object-Element as the content to solve this problem .. NET will reflect the Object-Element content to generate the corresponding Object and set it to the Object Property when profiling the Property-Element. Another reason for using Object-Element as the content of Property-Element is that Object-Element describes an Object and can have its own Property-Attribute and Property-Element, in this way, the tree structure of the object can be designed layer by layer.
 
The following section uses Property-Element to set the Foreground attribute of the TextBlock object. The Foreground attribute of the TextBlock object is an object attribute of the Brush type. Therefore, Object-Element is used in Property-Element to generate a Brush Object to be set to the Foreground attribute.
 
<TextBlock x: Name = "ShowTextBlock" Text = "Hello World" FontSize = "72">
<TextBlock. Foreground>
<SolidColorBrush Color = "# FF0000"/>
</TextBlock. Foreground>
</TextBlock>

 


 

Taking a closer look at the above example, we will find that it is not to generate a Brush object, but to generate an extended image of the Brush, the SolidColorBrush. This is because the Brush object is an abstract class and cannot be directly generated. Therefore, only SolidColorBrush extending from the Brush can be generated as the object of the Foreground attribute. In other words, we can generate extended categories to set object attributes. This is an important feature of object-oriented development. It provides the ability of developers to switch objects and greatly increases the elasticity of system objects.
 
Property-Element-Collection
Since the attribute of an object is not simply an int or double type, it may be an object (Class) or a structure (Struct ). It is inevitable that the attributes of an object may also be a Collection of objects and structures ). Property-Element is also defined. Multiple Object-elements can be used as the content to solve this problem .. NET, When profiling the Property-Element, multiple Object-Element content will be reflected to generate the corresponding Object, and added to the Object set attribute of the Object.
 
The following section uses Property-Element to set the GradientStops attribute of the LinearGradientBrush object. The GradientStops attribute of the LinearGradientBrush object is a GradientStop object set attribute. Therefore, multiple Object-elements are used in the Property-Element to generate multiple GradientStop objects to be set to the GradientStops attribute.
 
<TextBlock x: Name = "ShowTextBlock" Text = "Hello World" FontSize = "72">
<TextBlock. Foreground>
<LinearGradientBrush StartPoint = "0, 1" EndPoint = "1, 0">
<LinearGradientBrush. GradientStops>
<GradientStop Color = "# FF0000" Offset = "0.0"/>
<GradientStop Color = "#00FF00" Offset = "0.5"/>
<GradientStop Color = "# 0000FF" Offset = "1.0"/>
</LinearGradientBrush. GradientStops>
</LinearGradientBrush>
</TextBlock. Foreground>
</TextBlock>

 


 

Postscript

Learn about object generation in XAML. When learning development platforms such as WPF, Silverlight, and WP7, you can refer to the MSDN category library to query the objects generated through XAML. Measure the test taker's knowledge about the responsibilities and work content of the object, and set the changes to the object caused by each attribute. In this way, the learning route from the object itself will be correct and fast, and will not be confused by too many complicated changes.

 

From sleep Domains

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.