WPF笔记(1.1 WPF基础)——Hello,WPF!
//?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,?Avalon");
????}
??}
}
1。這里,在project中要事先導入3個framework的dll,分別是WindowsBase,PresentationCore,PresentatioFramework,這樣你才可以使用新的System.Windows——來自\Framework\v3.0\WindowsBase.dll,而不是\Framework\v2.0.50727\System.Windows.Forms.dll,從而增加了很多新的功能。
2。注意,vs2005下是看不到Main的,所以這么玩就不行;找到App.g.cs這樣的文件,Main代碼藏在這里,對其進行相應改動。vs2005下自動找Main的小技巧:因為App類是分散類,所以右擊函數定義,會找到兩個地方,一個就是本頁App.xaml.cs,另一個會定向到App.g.cs文件。
<!--?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.targets"?/>
</Project>
1。就是把*.csproj 工程文件用記事本打開看到的東西啦。相應命令行msbuild。總之,是vs2005所原有的。
2。倒數第二行有點意思,查了一下別人的blog,
| Microsoft.CSharp.targets位于C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727目錄下 |
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?=?new?MyApp(??);
??????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,?Avalon";
??????window.Show(??);
????}
??}
}
1。這個例子有語法問題,可能是寫書的時候還是WinFX,所以StartingUpCacalEventArgs事件應該改為StartUpEventArgs,?也可以不在Main里面做,
在App.xaml的Starting屬性指定就可以了。Window還沒有Text屬性,相應的要改為window.Title
2。MyApp:Application
看到這里,不得不說了。其實WPF分為兩種,一種是Window Application(C/S),使用Window標簽;另一種是Browser Application(B/S),使用Page標簽。但是WPF的Project,都用App.xaml文件作為入口,相應標簽是Application,app.xaml中寫Main函數,但是一般不可見,隱藏在app.g.cs文件中(分散類機制)。App.xaml的Application標簽中,用StartupUri屬性指定第一個打開的Form/Page是哪一個。具體的xaml語法見后。
//?Window1.cs
using?System;
using?System.Windows;
using?System.Windows.Controls;?//?Button?et?al
namespace?MyFirstAvalonApp?{
??class?Window1?:?Window?{
????public?Window1(??)?{
??????this.Text?=?"Hello,?Avalon";
??????//?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。寫到這里我要罵人了,初學者都會上當在這里。我是調試了半天沒有成功。原因很簡單,沒有搞清楚vs2005自動生成的一些代碼。一個是Main函數,不要用他的,自己寫app.Run(new Window1);還有就是window的InitializeComponent方法所在那個部分類,全部mark掉,不用那個初始化方法,這樣就不和加載新button沖突了。唉,其實vs也是好意,我們真正開發還是要用vs的,但是現階段學習用例,確實vs會造成困惑。
2。其實還有一種等價寫法,就是充分利用xaml中的聲明,如<Button x:Name="button1",這樣相應的后臺可以直接使用這個button1對象——xaml語言等價于對象建模。而這種方法的實質就是vs2005自動生成的InitializeComponent方法,它是加載這個xaml文件,將其序列化為對象,加載到Application級別中,接下來就可以使用了。
3。例1.7——1.13講的就是我上面所述的。總之這本書的寫作順序不對,應該指出來前面先不要用vs2005,而后講vs的玩法及原理,最后再展示vs上開發的例子——這樣就對了。
轉載于:https://www.cnblogs.com/lonelyxmas/p/4962697.html
總結
以上是生活随笔為你收集整理的WPF笔记(1.1 WPF基础)——Hello,WPF!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正式入住了
- 下一篇: 安卓TCP通信版本2