原文:
WPF笔记(1.1 WPF基础)——Hello,WPF!
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, 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文件。html
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.targets
"
/>
</
Project
>
1。就是把*.csproj 工程文件用记事本打开看到的东西啦。相应命令行msbuild。总之,是vs2005所原有的。
2。倒数第二行有点意思,查了一下别人的blog, windows
Microsoft.CSharp.targets位于C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727目录下 |
用记事本打开,是一个XML文件,记录了生成项目的全部步骤。
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语法见后。
app
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, 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上开发的例子——这样就对了。
less