Example 1 - 1 . Minimal C # WPF Application
// MyApp. CS
Using System;
Using System. windows; // The root WPF namespace
Namespace Myfirstavalonapp {
Class MyApp {
[Stathread]
Static Void Main () {
//The WPF message box
MessageBox. Show ("Hello, aveon");
}
}
}
1. Here, we need to first import three frameworks in the project, namely windowsbase, presentationcore, and presentatioframework, so that you can use the new system. windows -- from \ framework \ V3.0 \ windowsbase. DLL instead of \ framework \ v2.0.50727 \ system. windows. forms. DLL to add many new functions.
2. Note that main is not visible in vs2005, so it won't work if you do this. Find a file like app. G. CS, mainCodeClick here to modify it. Tips for Automatically Searching for main in vs2005: Because app classes are scattered classes, right-click the function definition and find two places. One is the app on this page. XAML. CS, the other will be directed to the app. g. CS file.
Example 1 - 3 . A minimal msbuild project file
<! -- 1st. csproj -->
< Project
Defaulttargets = " Build "
Xmlns = " Http://schemas.microsoft.com/developer/msbuild
/ 2003 " >
< Propertygroup >
< Outputtype > Winexe </ Outputtype >
< Outputpath > .\ </ Outputpath >
< Assembly > 1st.exe </ Assembly >
</ Propertygroup >
< Itemgroup >
< Compile include = " MyApp. CS " />
< Reference include = " System " />
< Reference include = " Windowsbase " />
< Reference include = " Presentationcore " />
< Reference include = " Presentationframework " />
</ Itemgroup >
< Import Project = " $ (Msbuildbinpath) \ microsoft.csharp.tar gets " />
</ Project >
1. Open the *. csproj project file in notepad. The corresponding command line msbuild. In short, it is the original version of vs2005.
2. The penultimate line is a bit interesting. I checked others' blogs,
Microsoft.csharp.tar gets is located in the c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 directory |
Open it in Notepad. It is an XML file that records all the steps for generating a project.
Example 1 - 5 . A less minimal WPF Application
// MyApp. CS
Using System;
Using System. windows;
Namespace Myfirstavalonapp {
Class MyApp: Application {
[Stathread]
Static Void Main ( String [] ARGs) {
MyApp app= NewMyApp ();
App. startingup+ =App. appstartingup;
App. Run (ARGs );
}
Void Appstartingup ( Object Sender, startingupcanceleventargs
E) {
// By default, when all top level windows
// Are closed, the app shuts down
Window window = New Window ();
Window. Text = " Hello, aveon " ;
Window. Show ();
}
}
}
1. This example has a syntax problem. It may be winfx when writing a book. Therefore, the startingupcacaleventargs event should be changed to startupeventargs or not in main,
You can specify the starting attribute in APP. XAML. There is no text attribute for the window, and the corresponding attribute should be changed to window. Title.
2. MyApp: Application
I have to say it here. In fact, WPF is divided into two types: Window application (C/S), using the window tag, and browser application (B/S), using the page tag. However, all WPF projects use the app. the XAML file is used as the entry, and the corresponding tag is application, app. write the main function in XAML, but it is generally invisible and hidden in the app. g. CS file (distributed class mechanism ). In the Application tag of APP. XAML, The startupuri attribute is used to specify which form/page is opened first. For details about the XAML syntax, see.
Example 1 - 6 . Window Class Declaring its own controls
// Window1.cs
Using System;
Using System. windows;
Using System. Windows. controls; // Button et al
Namespace Myfirstavalonapp {
Class Window1: Window {
Public Window1 () {
This . Text = " Hello, aveon " ;
// Do something interesting (sorta)
Button button = New Button ();
Button. Content = " Click me, baby, one more time! " ;
Button. Width = 200 ;
Button. Height = 25 ;
Button. Click + = Button_click;
This . Addchild (button );
}
Void Button_click ( Object Sender, routedeventargs E) {
MessageBox. Show (
"You 've done that before, haven't you",
"Nice!");
}
}
1. I want to swear when writing it here, so beginners will be fooled here. I failed debugging for half a day. The reason is very simple. I have not figured out the code automatically generated by vs2005. One is the main function. Do not use it. Write your own app. run (New window1); there is also the class in which the initializecomponent method of window is located, mark all, and no initialization method is needed, so that it will not conflict with loading the new button. Alas, in fact, VS is also a good intention. We still need to use vs for actual development. However, at this stage, it is true that Vs will cause confusion.
2. In fact, there is another equivalent method, that is, to make full use of the statements in XAML, such as 3. Example 1.7 -- 1.13 describes what I have mentioned above. In short, the writing sequence of this book is wrong. We should point out that we should not use vs2005 first, then talk about the gameplay and principles of Vs, and finally show the example developed on vs -- that's right.