Windows Presentation Foundation Series-recognition of WPF from helloworld

Source: Internet
Author: User
Tags reflector

 

Disclaimer: Anyone and organizations are welcome to repost the articles in this blog, but the original links and author information must be marked.

Link: http://blog.csdn.net/li_007/archive/2010/11/02/5982660.aspx

Pioneering little turtle -------> csdn

 

 

Okay, as a programmer, most people should be "Hello, world" for the first program in a brand new language. Here I also start from this, the simplest Hello world. The Code is as follows:

Using system; <br/> using system. windows; <br/> namespace helloworld <br/> {<br/> class helloworld <br/> {<br/> [stathread] <br/> Public static void main (string [] ARGs) <br/> {<br/> // console. writeline ("Hello, world! This is a most simple WPF application "); <br/>}< br/>} 

Use the command line to compile the file, as shown in the following code: CSC/out:./helloworld.exe helloworld. CS. After compilation is successful, Hello, world is displayed! This is a message box of a simple WPF application. In fact, this WPF is not complete and does not use any WPF service. In the end, what key components (or core DLL files) Does wpf have to do? We can understand it through the image below.

Figure 1

 

The three core components of WPF are highlighted in red. Among them, the milcore component is responsible for interacting with direct3d. In addition, for efficiency and security considerations, milcore is implemented by unmanaged code. All displays in WPF are completed by the DirectX engine, which enables efficient hardware and software presentation. WPF also requires precise control over Memory and execution. The composite engine in milcore is greatly affected by performance, and many advantages of CLR need to be abandoned to improve performance. The other two core components of WPF, presentationframework and presentationcore, are both located on the Common Language Runtime Library (CLR. As you can see, most of the WPF code is hosted. These two components provide the function libraries and function libraries required by the WPF project. Because they exist in the form of hosting, they also avoid direct operations on the underlying layer and the possibility of Memory leakage.

Figure 2

 

From the figure above, we can see that it is divided into five parts (core presentation, user interface services, base services, document servies, and XPS viewer ):

  • Core presentation:Contains all the graphic effects, shapes, 2d, 3D, text, audio, video, and display effects. It also includes powerful animation effects, which can be applied to all the elements in the front. The bottom is the basic element of vision.
  • User Interface services:Includes application services, deployment services, control libraries, layout, and data binding.
  • Base services:It provides support for XAML, improved development efficiency, input and event, and attribute systems.
  • Document servies and XPS ViewerThe basic printing and reporting services are provided. You can use these components to customize the printing and display effects.

 

Now, we can continue to improve Hello world. The improved code is as follows:

 

Using system; <br/> using system. windows; <br/> namespace helloworld <br/> {<br/> class helloworld <br/> {<br/> [stathread] <br/> Public static void main (string [] ARGs) <br/> {<br/> // MessageBox. show ("Hello, world! This is a simple WPF application "); </P> <p> window WND = new window (); <br/> WND. width = 800; <br/> WND. height = 600; <br/> WND. title = "Hello World"; <br/> WND. show (); </P> <p> application APP = new application (); <br/> app. run (); </P> <p >}< br/>}

Compile the command through the command line again. The compilation command is as follows (for ease of understanding, I typeset the compilation command, which is not required in actual use)

 

 

CSC/Target: winexe/out:./helloworld.exe

/Reference: "C:/program files/reference assemblies/Microsoft/framework/V3.0/presentationframework. dll"

/Reference: "C:/program files/reference assemblies/Microsoft/framework/V3.0/windowsbase. dll"

/Reference: "C:/program files/reference assemblies/Microsoft/framework/V3.0/presentationcore. dll"

Helloworld. CS

 

 

Note that the prensentationframework. dll, presentationcore. dll, and windowsbase. dll are referenced here.

Use. Net reflector (http://reflectoraddins.codeplex.com/) here to view the Il code of the compiled EXE file as follows:

Figure 3

 

From, we can clearly see that the compilation process of the WPF program, and we can see that it links those WPF components.

Of course, a real WPF program is much more than a message dialog box or a simple form with nothing left. It requires an instance of the application class. Here we will inherit an application and Windows respectively. The specific code is as follows:

Using system; <br/> using system. windows; <br/> namespace helloworld <br/>{< br/> class helloworld: application <br/> {<br/> [stathread] <br/> Public static void main (string [] ARGs) <br/>{< br/>/* # region // simple I <br/> MessageBox. show ("Hello, world! This is a simple WPF application "); <br/> # endregion // simple I </P> <p> # region // simple II <br/> window WND = new window (); <br/> WND. width = 800; <br/> WND. height = 600; <br/> WND. title = "Hello World"; <br/> WND. show (); </P> <p> application APP = new application (); <br/> app. run (); <br/> # endregion // simple II <br/> */<br/> # region // simple III <br/> helloworldwnd WND = new helloworldwnd (); <br/> WND. show (); </P> <p> helloworld = new helloworld (); <br/> helloworld. run (); <br/> # endregion // simplw III <br/>}</P> <p> class helloworldwnd: window <br/>{< br/> Public helloworldwnd () <br/>{< br/> This. width = 400; <br/> This. height = 300; <br/> This. title = "Hello World windows"; <br/>}< br/>}

Use reflector to view the Il Code as follows:

Figure 4

 

Figure 5

The above two figures are respectively the helloworld class inherited from the application and the Il code of the helloworldwnd class inherited from the window. Comparison Figure 3 is basically the same process, they are only two classes.

However, through the helloworld and helloworldwnd classes at the end, we can see that the WPF code can be separated, and the "behavior" of WPF is implemented in helloworld "; helloworldwnd implements the "appearance" of WPF ". In this way, we can focus on different things separately. For example, designers can focus on uidesign, while software engineers can focus on program logic processing. Well, if we define a declarative format for the "appearance" code of WPF, and files in this format can be created by dragging and dropping tools, then perfect will be ready. Is it a bit like flex? Well, we are lucky. MS has implemented this for us. The specific format in WPF Is In The XAML format. The code for using XAML to implement the helloworldwnd class is as follows:

<! -- Helloworldwindow. axml --> <br/> <Window X: class = "helloworld. helloworldwnd "<br/> xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "<br/> xmlns: X = "http://schemas.microsoft.com/winfx/2006/xaml" <br/> Title = "Hello World windows" <br/> width = "400" Height = "300"> <br/> </WINDOW> 

In fact, it is very simple. A declarative syntax simply describes the features of windows. The XAML Specification defines some rules to map. Net namespaces, types, attributes, events, and other elements into the namespace, elements, and features of the XAML. XAML is designed as direct ing as much as possible. For example, a XAML element is mapped to one. net class name. a xaml attribute is mapped to the property name of the corresponding class or the corresponding event name of the class. In this way, the XAML is not only used by the WPF class, but also allows the. NET class with the default constructor to complete initialization directly in the XAML file. For example, the window class above

 

Let's take a look at the structure of a WPF Application project created in Visual Studio. NET 2010, as shown below:

Figure 6

 

From the standard WPF program structure diagram in figure 6, it can be seen that WPF requires support from three core components: presentationcore, presentationframework, and windowsbase; relationship between XAML and code-behind files; the role of the keyword partial in WPF.

 

The extension of the code-behind file is. XAML. CS, which contains undefined parts in XAML. The keyword partial is used to make the compiler combine it with the XAML definition into a complete class definition. Generally, a "behavior" of a class is processed in a code hidden file, for example, an element is an event.

 

The above example shows the basic framework of the WPF program. Of course, you need to query the msdn and other information on your own.

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.