Introduction to WPF XAML first glimpse

Source: Internet
Author: User
Tags xml attribute xmlns silverlight

Overview: Very early to write some articles to sum up their own learning some new knowledge experience, one can be found in the future, its role in an index, and the love of technology and friends to communicate, in view of the work is always unable to get out of writing. Recently derived from the intense interest in WPF and Silverlight and the use of projects, I hereby share some of the learning wpf/silverlight process. This series will briefly review the basic concepts and getting started in WPF to form the basis for creating WPF programs (even Silverlight programs, largely dependent on WPF). I have not written a lot of blogs, and also WPF itself to some of the programmer's innovative thinking challenges to me is a great difficulty, and I would like to ask you heroes. Thank you very much.

Topic: This article the first of this series, will be roughly introduced in the. The concepts of XAML that are officially enabled in NET3.0 and their application in WPF.

It is generally known that with the advent of the. NET Framework 3.0, it contains several components of Windows Workflow Fundation, Windows Communication Foundation, Windows CardSpace and the Windows Presentation Foundation we're talking about are gradually attracting a lot of attention, as part of the. NET Framework, It provides a lot of advantages, so that our programming model to become ideas have changed a lot. In its main advantages, the introduction of the "declarative description" brings us a fresh feeling, and this is the main credit of XAML. XAML as a declarative xml,.net framework that can largely replace some coding statements provides compiler support for XAML, which in turn allows us to see a similar style to Html/xml, which seems casual, A very flexible style brings us a new programming experience. WF, WPF uses XAML to describe part of the process.

1. What is XAML?

XAML is a relatively simple, generic declaration that becomes language and is suitable for building and initializing. NET object. It consists of some rules and keywords, but it does not have any meaningful elements of its own. Discussing XAML on the basis of no WPF/WF framework is no more meaningful than discussing C # on the basis of No. NET Framework, as it does not provide any meaningful elements in itself, and requires the WPF/WF framework or. NET XAML compilers and WPF application frameworks to host and understand the meaning of XAML expression. Because of the versatility of XAML, you can actually apply it to any. NET technology. However, the use of XAML in WPF is optional, and the things that each XAML can do can be done by any one of your favorite. NET languages, but the reverse is not possible. As you can see in the explanation below, the XAML compiler is actually associated with a XAML statement with a. NET object/type, and controlling the XAML into the same model as the. NET type is an important reason why they can be compatible with each other.

For example, the following XAML fragment is the same as the result expressed by the. NET C # statement:

XAML: <button xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation content= "OK"/>

C #: System.Windows.Controls.Button Btnok = new System.Windows.Controls.Button ();

B.content = "OK";

2. XAML elements and Attributes 2.1 XAML namespaces

The object element of a XAML file must have at least one XML namespace to authenticate itself and its child elements. You can declare additional XML namespaces on the root element or child element to identify your custom elements, but identifiers under each namespace must have a unique prefix. We take http://schemas.microsoft.com/winfx/2006/xaml/presentation as the default (primary) namespace, and we use this primary namespace when using most controls, Because WPF maps this namespace to the. Net namespace in a hard-coded fashion. In the case of a secondary namespace, we usually need to add a prefix to indicate that its namespace is referenced in the following scope, like the using System in C #, declaring a namespace that can be referenced. The following code fragment demonstrates how to use the primary namespace and the secondary namespace:

<UserControl x:Class="MediaPlayer.Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="450" Height="300">
  <Grid x:Name="LayoutRoot" Background="White">
    <TextBlock FontSize="12" FontWeight="Bold" HorizontalAlignment="Center">Vedio Player</TextBlock>
  </Grid>
</UserControl>

2.2 Attribute elements (property element)

Property elements are essentially a way to increase the inclusion of child elements in an element. It is a method that XAML provides to override a more detailed syntax to set property values. For example:

<Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" >
  <Button.Content>
    </Rectangle Height="40" Width="40" Fill="Black" />
  </Button.Content>
</Button>

The Content property is now set to an XML element instead of an XML attribute, and a period in button.content is used to differentiate between object elements and attribute elements. Property elements are always present in the form of the type name. property name and are included in the type name object element. To be blunt, an attribute element exists to extend a type of property.

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.