Basic Windows Phone XAML tutorial

Source: Internet
Author: User
Tags addchild class definition xmlns silverlight

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:

<UserControl x: Class = "SilverlightApplication1.MainPage"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = http://schemas.microsoft.com/expression/blend/2008
Xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Mc: Ignorable = "d"
D: DesignHeight = "300" d: DesignWidth = "400">
<! -- Http://schemas.microsoft.com/winfx/2006/xaml/presentation:core silverlightnamespace
Http://schemas.microsoft.com/winfx/2006/xaml:is the xamlnamespace
-->
 
<Grid x: Name = "LayoutRoot" Background = "White">
<Button
X: Name = "button"
Width = "200"
Height = "25"
Click = "button_Click"
            >
Click me, baby, one more time!
</Button>
</Grid>
</UserControl>

The running result is shown in the following figure:


C # code equivalent to the XAML code:

The code is as follows:

Partial class MainPage: UserControl // class MainPage inherits from UserControl
     {
Button button; // declare a Button class object

Void InitializeComponent ()
         {
// Initialize the button
Button = new Button ();
Button. Width = 200;
Button. Height = 25;
Button. Click + = button_Click; // add an event

This. AddChild (button); // call AddChild () to add the button to the current object.
        }
    }

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

This 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:

The code is as follows:

<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.

The code is as follows:

<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, bytes

Button, usually, this namespace is not declared using the namespace prefix, so it becomes the default namespace for the entire XAML document, that is, unless you specify it, each element is automatically placed in this namespace.

Medium.

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:

The code is as follows:

<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:

Now.

The code is 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:

The code is as follows:

<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 named

MainPage. 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

Use common object construction syntax, such:

The code is as follows:

MainPage myMainPage = new MainPage ();

 

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.