Use the CSC compiler to generate the WPF Hello World Program

Source: Internet
Author: User

After studying for more than three hours, I finally got it done. After writing my blog, I must go to a break ....

Csc, I believe some people will understand or use it. Csc is the C # compiler. It uses some parameters to compile the CS file as an executable file.

Baidu has some content on csc, but there is no compilation on the csc compiler to generate the WPF application. Therefore, this article is not repeated.

Use csc to compile the WPF program for two purposes:

1. this helps improve your understanding of WPF. This is quite different from creating a WPF project using a WPF template. When you create a WPF program using a template, the Application and Window windows Windows have long been generated for you, although convenient, the final result is that you don't even know how WPF runs.

2. You can use this method when programming is required on a computer without VS installed but with. Net installed. (This is just a simple example. You can continue to study other things)

When using CSC, you first need to upload the csc.exe file path (located in your system.. Net installation directory of any version, for example, C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319) to the path of the system environment variable, as long as you have learned Java, it should be configured and will not go to Baidu.

After the configuration is complete, (re-) Open the command prompt and locate the folder where you want to compile the program. To compile a C # program, you must have a cs file. Create a text file and name it 1.cs. open it in the text editor (notepad is also integrated), enter the C # code in it, and then enter the following in the command prompt:

Csc 1.css

If there is no error, the exe executable file will be generated in the folder of your Compilation Program. Assume that we enter

using System;class Program{static void Main(){    Console.WriteLine("Hello World");     Console.Read();}}

Compile it with the css compiler and generate an exe file. Open it and you will find a console application and output Hello World, which indicates that our program has been compiled successfully.

This just gives you a brief introduction to the csc compiler. Before using csc to compile the WPF program, you must first talk about the basic syntax parameters of the csc Compiler:

The file type generated by/targert. For details, see:

/Target: exe

Create a. exe file.

/Target: library

Create a code library.

/Target: module

Create a module.

/Target: winexe

Create a Windows program.

/Target: winmdobj

To create an intermediate. winmdobj file.

For example, enterCsc/target: exe 1.cs, An exe console program is generated.

The default value is/target: exe. Therefore, the console program above is successfully compiled without adding any parameters. If the WPF program is compiled, select/target: winexe.

 

/Reference (/r) references the Assembly File

 

For example, enterCsc/r: System. dll/target: exe 1.csTo load and reference System. dll.

 

Currently, the above two are used. For other information, refer to the content in msdn or Baidu. Now we start to construct our WPF program.

As we all know, WPF has several basic assembly types: PresentationCore. dll, PresentationFramework. dll and WindowsBase. dll, and also reference System. xaml. dll (the last one is in the same folder as the csc compiler, and the first three are in the subdirectory WPF), so they should be referenced during compilation.

 

Compile our C # code:

 

Using System; using System. windows; using System. windows. controls; using System. windows. media; using System. windows. media. animation; public partial class App: Application {[STAThread] // indicates a single thread unit public static void Main () // Main function entry {App app = new App (); // create an App object that inherits from the Application. run (); // Run it} Grid grid Grid; // indicates the Grid Window w in the Window; // indicates a Window protected override void OnStartup (StartupEventArgs e) // triggered at App startup {base. onStartup (e); w = new Window (); // instantiate the Window grid = new Grid (); // instantiate the Grid w. title = "Hello World Application"; w. content = grid; // set the Content of the Window to a Grid w. width = 300; w. height = 200; w. loaded + = MainWindow_Loaded; w. show (); // display window} void MainWindow_Loaded (object sender, RoutedEventArgs e) {TextBlock tb = new TextBlock (); // create a TextBlock control tb. text = "Hello World"; // set the Text grid. children. add (tb); // Add it to the Grid: TranslateTransform tt = new TranslateTransform (); // create a translation transformation object tb. renderTransform = tt; // associate it with TextBlock control tb. renderTransformOrigin = new Point (0.5, 0.5); // set the transformation Point DoubleAnimation da = new DoubleAnimation (); // create an animation da. to = w. actualWidth-tb. actualWidth; // set the To attribute da of the animation. duration = TimeSpan. fromSeconds (10); // sets the animation duration da. repeatBehavior = RepeatBehavior. forever; // execute the animation Storyboard repeatedly. setTarget (da, tb); Storyboard. setTargetProperty (da, new PropertyPath ("RenderTransform. X "); Storyboard story = new Storyboard (); story. children. add (da); story. begin (); // start animation }}

 

The Code adds an animation of the sign in WPF. To avoid your ignorance that it is a WPF Program

Start to compile the code and enter:

Csc/target: winexe/r "WPF \ PresentationCore. dll", "WPF \ PresentationFramework. dll", "WPF \ WindowsBase. dll", "System. Xaml. dll" 1.cs

 

Run it. If the code is correct, the WPF application will be compiled. I wonder if you think this compilation command is a little long and not conducive to input? Is there a simple way, such as writing the list of referenced assembly into a configuration file? The answer is yes, but it is not just a configuration file.

If you are careful enough, you will find that the above console program directly using without referencing the dll of the Assembly file. Does the compiler automatically find and reference it? Of course not. In the csc folder, there is a file named csc. rsp, open it with notepad, there are many Assembly references, including most of the commonly used assembly, but there is no WPF-related assembly, so we will be several of the WPF core assembly (PresentationCore. dll, PresentationFramework. dll, WindowsBase. dll) to csc. in the rsp file, how to add it? Please open csc. rsp knows, and the path entered in the command prompt is the same. (It is worth noting that there may be an administrator privilege problem in the operating system above Windows Vista. copy the file to another place and edit it to overwrite the original file. If not, add the permission to modify the user you are using)

Specific content added to csc. rsp:

/R: "WPF \ PresentationCore. dll"
/R: "WPF \ PresentationFramework. dll"
/R: "WPF \ WindowsBase. dll"
/R: "System. Xaml. dll"

 

After the configuration is complete, you can enter

Csc/target: winexe 1.cs

Press enter to complete compilation of the WPF program. Run it and see if it is successful?

 

This article has taken me a lot of time and I hope it will help you. If you think it is not well written, please submit it in the comments. I will withdraw from the homepage. Thank you for your cooperation !!!!

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.