原文:
WPF笔记(1.1 WPF基础)——Hello,WPF!
Example
1
-
1
. Minimal C# WPF application
//
MyApp.cs
using
System;
using
System.Windows;
//
the root WPF namespace
![img_405b18b4b6584ae338e0f6ecaf736533.gif](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
namespace
MyFirstAvalonApp
{
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
class MyApp
{
[STAThread]
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
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文件。
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,
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;
![img_a6339ee3e57d1d52bc7d02b338e15a60.gif](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
![img_405b18b4b6584ae338e0f6ecaf736533.gif](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
namespace
MyFirstAvalonApp
{
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
class MyApp : Application
{
[STAThread]
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
static void Main(string[] args)
{
MyApp app = new MyApp( );
app.StartingUp += app.AppStartingUp;
app.Run(args);
}
![img_33d02437d135341f0800e3d415312ae8.gif](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
void AppStartingUp(object sender, StartingUpCancelEventArgs
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
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语法见后。
Example
1
-
6
. Window
class
declaring its own controls
//
Window1.cs
using
System;
using
System.Windows;
using
System.Windows.Controls;
//
Button et al
![img_405b18b4b6584ae338e0f6ecaf736533.gif](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
namespace
MyFirstAvalonApp
{
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
class Window1 : Window
{
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
public Window1( )
{
this.Text = "Hello, Avalon";
![img_33d02437d135341f0800e3d415312ae8.gif](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
// 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;
![img_33d02437d135341f0800e3d415312ae8.gif](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
this.AddChild(button);
}
![img_33d02437d135341f0800e3d415312ae8.gif](https://yqfile.alicdn.com/img_33d02437d135341f0800e3d415312ae8.gif?x-oss-process=image/resize,w_1400/format,webp)
![img_2887d91d0594ef8793c1db92b8a1d545.gif](https://yqfile.alicdn.com/img_2887d91d0594ef8793c1db92b8a1d545.gif?x-oss-process=image/resize,w_1400/format,webp)
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上开发的例子——这样就对了。