Introduction to Getting Started with WPF

Source: Internet
Author: User

Windows Vista has been officially released in January 2007 with a retail version of 30, and the installation of Vista computers will appear in large numbers. In the Vista era, as a programmer, you must have the ability to develop Vista desktop applications. And the most important thing to develop Vista desktop applications is WPF. This series of articles organizes the essence of WPF technology and, in a serialized way, describes how to write Microsoft Windows platforms using WPF.

What is WPF?
WPF is the abbreviation for Windows Presentation Foundation. WPF is a set of APIs that can be developed using the WPF program:
A Windows app that is typically executed independently, also known as a client app.
Or the front end of a distributed (distributed) application--there is now a more fashionable name for rich Internet application (RIA).

While WPF is an app for writing Windows platforms, WPF apps are definitely different from previous Windows apps: Outside, WPF apps have a new look and new graphics effects (including animations and 3D), and within, WPF apps have a novel control design philosophy and new APIs.
WPF apps can only be executed in the. NET Framework 3.0 environment and must be installed on the computer:
Windows Vista (comes with the. NET Framework 3.0) or
Windows XP/SP2 or
Add Windows 2003 for the. NET Framework 3.0.

If you are a programmer, your environment, in addition to the above requirements, must also be equipped with:
Visual Studio 2005 Professional Edition (commercial software)
. NET Framework 3.0 and Windows SDK (free)
Visual Studio Extensions for WCF, WPF (free)
The free software listed above can be downloaded from Microsoft's MSDN website, and in addition, Visual C # has launched Express Edition and can be downloaded for free. You may want to use the free Visual C # Express Edition instead of professional Edition, but visual Studio Extensions for WCF, WPF can only be paired with professional Visual Studio 2005 for edition. Because the. NET Framework SDK that is included with Visual Studio 2005 (and Visual C # 2005 Express Edition) is version 2.0, you must install the. NET Framework 3.0 with additional visual Stu Dio Extensions for WCF, WPF. It is estimated that Microsoft will launch a new version of Visual Studio by the end of 2007, as long as you install Visual Studio directly, not as hard as it is now, to get together and download and install software everywhere.

If you can't get Visual Studio 2005 Professional Edition and don't want to install the huge Windows SDK (which is scary and requires 1.15GB), then take advantage of the free visualc# Express Edition and the free. NET Framework 3.0 can write a WPF program just as well, with two drawbacks:
In Visual C # without the WPF template, you must manually write each line of the program and manually add references to the associated DLLs. (This article later describes how to add DLL references.) )
You cannot use XAML.

History of Windows Development technology
Windows has a history of more than 22 years, and for 22 years, Microsoft's official main thrust of the programming language and API has four watershed:
1985-1991 years: C paired with Windows API. Currently we seldom write programs with the C and Windows APIs, but it is still necessary to be familiar with such techniques, because there are some special times to use.
1992-2001 years: C + + with MFC link library (this time is also the most beautiful Visual Basic time). Historically, MFC was the most human-use Windows programming method. I'm glad MFC's so bad technology has finally faded out of history!
2002-2006 years: C # paired with Windows Form. The focus of IT technology during this period is on the web, so while everyone is using. NET, the real protagonist is ASP., not Windows Forms. Windows forms has not yet waited for its early appearance, and WPF appears. Windows forms is destined to be the "least-used Microsoft development Technology" in history. Windows Forms is actually a good technology, but the end of good Technology (Windows Forms) is no one to use, poor technology (MFC) is the end of everyone in use. I can only say that Windows forms untimely.
2007~ today: C # is paired with WPF. You may find it odd that Microsoft has released two. NET APIs that are similar, but different and incompatible. In fact, Windows forms is designed with a preference for traditional applications (similar to Java Swing), and does not take into account the web/markup requirements, so Microsoft will then design a new WPF.

Windows Form vs WPF vs Apollo
Because Windows Forms has evolved over the years, control and Standard Dialogs (dialog box) are better than WPF, but the future of WPF is promising. Especially:

If you need to do a lot of custom controls and drawings, WPF is more appropriate than Windows Forms. While WPF and Windows Forms are developed, they can be "visually" and "logically" cut, but the visual part of Windows Forms uses the partial Class, but the visual part of WPF is XML (XAML). The XML format is suitable for programmers who do not understand programming, and Microsoft will launch the relevant design tool (Expression) this year (2007).
WPF intends to take advantage of the simplified version wpf/e into a variety of devices, including browsers and mobile phones. Currently, Windows Forms can be used on Windows Mobile 5, but it cannot be used within a browser. WPF/E can be used on Windows Mobile 6 and in various browsers.

Adobe Apollo and WPF are similar technologies. If you want to develop cross-platform applications, of course choose Adobe Apollo (another option is Java Swing, but before the F3 language is officially released, I don't think Java is a good choice), if you want to develop a Windows platform-specific application, of course choose WPF, because:
When developing an application, I occasionally need to call the OS DLL, but Apollo 1.0 does not allow us to do so (for security reasons).
If you are certain that your application does not require cross-platform, the adoption of the development methods supported by the operating system can often lead to other marginal benefits. For example, using WPF to develop Vista apps makes it easy for apps to be compatible with the power Shell, and using cocoa to develop MacOS X applications makes it easy for applications to be compatible with AppleScript.

C # VS XAML
A generic WPF application will contain both:
Program code (this series of articles uses C #): Primarily the part that handles logic.
XAML (an XML language): Primarily a part of the visual process. The English pronunciation of XAML is zammel.
Combining the advantages of XAML and C # program code can help us build larger, more complex applications. You can refer to my previous issue (February 2007) of the Apollo article, C # is actually similar to ActionScript, while XAML is similar to Mxml. Any WPF program can be developed in C # only, without XAML. And only a small number of simple programs can be used only in XAML, completely do not use C # to develop. From this point of view, C # program code is the focus, and XAML just makes it easier for us to program. Everything that is described in XAML is translated into program code at compile time. So, if you really want to master the technology of WPF, then in the learning phase, you should put your mind first in the part of the program code. Each WPF programmer should establish a solid foundation for the program code portion of WPF.

XAML files are typically produced using interactive tools such as expression and programming tools such as visual Studio, and we should naturally use such tools, after all, that XML is meant to be read by machines. However, it is still necessary for each programmer to have the ability to write XAML in person.

WPF Namespaces
When developing WPF applications, pay particular attention to the following namespaces (namespace):
System.Windows: This namespace contains all the basic WPF categories, structs, interfaces (interface) , delegate, and enumeration (enum), which contains the application and window categories. WPF development applications, in general, need to establish application objects and window objects at the outset.
System.Windows: Its WPF namespace begins with System.Windows, such as System.Windows.Controls, System.Windows.Input, System.Windows.Media.
System.Windows.Forms: Although this namespace is under System.Windows, it is not part of WPF, but is the most basic Windows Forms namespace. The
other Windows Forms namespaces are fairly fragmented, including System.Windows.Forms, System.Drawing, and System.ComponentModel.
System.Windows.Forms.Integration: The category of this namespace is used to facilitate integration between Windows Forms and WPF program code.

A simple WPF Application
Application and window can be said to be the most basic WPF category, both of which belong to the System.Windows namespace. As the name implies, application is used to represent the entire application, and window is used to represent a windows.
In one application, only one application object can be created, and for other parts of the application, the Application object acts like a fixed ship's anchor. You can't see the Application object on the screen, but you can see the Window object.
The Window object appears on the screen, which is the normal Windows operating system windows, with a header column. The title column of the System menu icon is on the left, while the minimize, maximize, and Close window icon is on the right. This window has a border that resizes the window, and the large area of the window is occupied by a client area.
Basically, building the simplest WPF application is:
1. Create a window
2. Display window
3. Let the window enter the message loop (message loop) (through application assistance)

In fact, the traditional c/windows API, c++/mfc, c#/windows form is also such a step. Based on the three steps above, the following WPF program code is written. This program code is quite simple and useless to inherit:

Using System;
using System.Windows;
Namespace Wpftest
{
      class Hello
      {
         //Program entry point
      [STAThread]
      public static void Main ()
      {
          //1. Create window
      windows win = new Window ();
     //Set window title
      win. Title = "Hello";
     //2. display window
      win. Show ();
     //Build Application Object
      Application app = new Application ();
     //3. Let the window enter the message loop
      app. Run ();
     }
     }
}

Main () is the program entry point, and it doesn't matter which category the main () is placed in. The front of main must have [STAThread] attribute, or the compilation will fail. [STAThread] is the thread model used to specify that the initial application thread is single-threaded apartment. STA is the technical remnants of the former COM era, it is not necessary to understand it, [STAThread] roughly means: Our application does not use a multi-thread originating from the "execution environment".

In the Hello program, main creates an object of the window category, which is used to create a standard application window. The Title property is the text that appears in the Title column of the window, and the Show method displays the window on the screen.
The most important step here is to call the Run method of the Application object. In traditional Windows programming thinking, this is done to create a message loop that allows the application to receive input from the user's keyboard or mouse. If this program is executed on a Tablet PC, the app will also receive input from the stylus (stylus). Once the Run method is called, it is not returned until the window is closed. Then, the main method ends, and then the Windows operating system does some cleanup work.
Run () has another version that receives a Window object as a parameter. Run () checks to see if the window is already displayed, and if it is not already displayed, run () will call the Window object's show (). Then before entering the message loop, so these two lines of program:

Win. Show ();
App. Run ();

Can be simplified as follows:

App. Run (Win);
Using Visual C #

If you are using Visual Studio 2005 Professional Edition and you have a WPF extension installed, you can take the following steps to establish this Hello program:
1. From the "File" menu, select "New Project. In the New Project dialog box, choose Visual C #, Windows Presentation Foundation, and empty project, name the project Hello, and then press OK.
2. In the Solution Explorer on the right, the References area must contain PresentationCore, PresentationFramework, System, and WindowsBase. If these DLLs do not appear in this "References" area, please manually add them, simply right-click "References" and choose "Add Reference" (or, from the Project menu, choose the "add Reference "). In this "ADD Reference" dialog box, click on ". NET" tab, then select the necessary DLLs and press "OK" at the end.
3. In Solution Explorer, right-click the project name of Hello and choose New Item from the Add menu (or, from the Project menu, choose Add New Item). In the ADD New Item dialog box, choose Code file, type the file name "Hello.cs", and finally Press "OK".
4. Type the previous program code into the Hello.cs file.
5. From the "Debug" menu, choose "Start Without Debugging" (or press CTRL+F5 directly) to compile and execute the program.

If you use Visual C # 2005 Express Edition, the steps are the same as above, but pay special attention to step 2. You must manually join the following DLLs:
PresentationCore.dll (version 3.0.x.x)
PresentationFramework.dll (version 3.0.x.x)
System.dll (version 2.0.x.x)
WindowsBase.dll (version 3.0.x.x)
These DLLs are located in the deep directory of the GAC (c:/windows/assembly/). Since you cannot add the GAC DLL to a project in Visual C # (this is because shfusion's masking effect causes Visual C # to not see the archive structure in the GAC), you must first use the "DOS window" (Command prompt character window) to take advantage of Dir, CD, Copy the files (this takes a little time), copy them to a directory (for example, c:/assembly/), and then add them to the project as a reference (references):
1. On the right "solution Explorer, right-click on "References" and choose "Add Reference",
2. Turn to the "Browse" page and find PresentationCore ... File directories, select the files, and then press OK.
In the future if you need to use other. NET 3.0 APIs, you can also add their DLLs to the reference in the way described above.
Close and open the console window
in the case of Hello, you will see that there is a console window at the same time as the execution, because you can adjust the flag in the project's property by compiling a flag-setting relationship. This is done as follows:
1. Click on the project name on the right with the mouse and choose "Properties" from the menu (another option is to choose "Properties" from the "Project" menu).
2. Now you can check the various settings of the project, or you can change the settings. It is important to note that the "Output Type" is set to "Console Application", and obviously this setting does not prevent the Console program from further building the GUI window.
3. Set "Output Type" to "Windows application", this program will be as smooth as before, and this time the console window will not appear again.

In the development phase, the console window is sometimes our best friend because:
We can use it to display some textual information in order to debug.
If there are too many bugs in the program, or even a problem at first, and you cannot display the GUI window or enter an infinite loop, you can easily end the program by typing CTRL + C in the console window.


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/blpluto/archive/2007/11/11/1879108.aspx

Introduction to Getting Started with WPF

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.