Windows Phone development path (4) xaml basics (I)

Source: Internet
Author: User

As I mentioned in the previous post, XAML is a language based on XML to create and initialize. Net objects. Although XAML can be used in more CLR types, in Silverlight, it describes the UI in a way that humans can create.

I. First recognized XAML:

XAML code:

1 <usercontrol X: class = "silverlightapplication1.mainpage"
2 xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns: D = http://schemas.microsoft.com/expression/blend/2008
5 xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
6 MC: ignorable = "D"
7 d: designheight = "300" D: designwidth = "400">
8 <! -- Http://schemas.microsoft.com/winfx/2006/xaml/presentation:core silverlightnamespace
9 http://schemas.microsoft.com/winfx/2006/xaml:is the xamlnamespace
10 -->
11
12 <grid X: Name = "layoutroot" background = "white">
13 <button
14 x: Name = "button"
15 width = "200"
16 Height = "25"
17 click = "button_click"
18>
19 click me, baby, one more time!
20 </button>
21 </GRID>
22 </usercontrol>

Running result ,:

C # code equivalent to the XAML code:

1 partial class mainpage: usercontrol // class mainpage inherits from usercontrol
2 {
3 button; // declare a button class Object
4
5 void initializecomponent ()
6 {
7 // initialize the button
8 button = new button ();
9 button. width = 200;
10 button. Height = 25;
11 button. Click + = button_click; // Add an event
12
13 This. addchild (button); // call addchild () to add the button to the current object.
14}
15}

From the simple example above, we can come up with the following two points:

1. Generally, a XAML element is. net class name. a xaml attribute is the property name of a class or the event name of a class. XAML is designed as from XML. net Direct ing. this recognition is very important.

2. As I mentioned when defining XAML in the previous article, the XAML language is based on the XML language, so its representation is very similar to XML.

After understanding the above two points, we will start learning about XAML.

To facilitate the subsequent content, I will create another basic XAML example here as a public instance of the following content:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Grid>
<Ellipse Fill="LightBlue"/>
<TextBlock>
Name:<TextBlock Text="{Binding Name}"/>
</TextBlock>
</Grid>
</UserControl>

 

Ii. namespace:

When the <textblock> element is used in a XAML file, the Silverlight parser identifies an instance of the textblock class you want to create. however, it does not need to know what the textblock class is. after all, even if the Silverlight namespace only contains a single class named textblock, no one can guarantee that you will not create a class with the same name. specifically, you need a method to specify the Silverlight namespace information to explicitly use elements. this is why the namespace should be defined.

In Silverlight, it is important to understand the class by ing the XML namespace to the Silverlight namespace.

Next I will take the above example to explain.

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
</UserControl>

Here, the root element <usercontrol> defines two basic namespaces (of course, other namespaces will be introduced later ).

1, large.

2, middle.

 

The above two namespaces basically allow you to access the core library of the Silverlight element. however, if you do not think it is enough, you can also customize the namespace. The following is the syntax of the custom namespace.

Syntax:

<Usercontrol X: class = "silverlightapplication1.mainpage"

Xmlns: W = "CLR-namespace: widgets; Assembly = widgets"

...

The XML namespace Declaration sets three items:

1. xml namespace prefix. do not conflict with the prefix of another namespace.

2.. Net namespace. In this example, the. Class is placed in the widgets namespace.

3. assembly. in this example, the class used is widgets. part of the DLL assembly. assuming that you have added a reference to the widgets assembly in the Silverlight application, it will be automatically included in the final xap package. once.. Net namespace ing to XML namespace, you can use it anywhere in the XAML document. for example, if the widgets namespace contains a control named hotbutton, you can create an instance as follows.

<W: hotbutton text = "Click me" Click = "dosomething"> </W: hotbutton>

 

3. Generate a class:

There is an X: class attribute in the root element, which is shown again here:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
</UserControl>

I will explain the following points:

1. the prefix "X:" is the abbreviation of standard XML. It indicates that this specific attribute is specified by the xmlns: X attribute in the namespace.

2. For the XAML compiler, the X: class attribute means that the class definition based on the XAML file needs to be generated, and the X: Class Attribute determines the generated class name, and derived from the root element. the generated class is mainpage, and its base class is usercontrol.

3. You do not have to specify an X: class attribute. if we ignore the attributes in this example, the root object type will be usercontrol, rather than the generated mainpage class. however, this attribute is usually specified.

4. When you select to generate a class, it provides a simple method to create an object tree using the XAML description. since every mainpage instance we generate will contain a series of objects specified by XAML, we only need to use common object construction syntax, such:

Mainpage mymainpage = new mainpage ();

The above is what we have summarized today. I hope that through this article, I can have a preliminary understanding of XAML and lay a solid foundation for further study!

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.