1.1 从头认识WPF_1.1 WPF from Scratch

1.1. WPF from Scratch

Example 1-1 is pretty much the smallest WPF application you can write in C#.

--------------------------------------------------------------

示例1.1几乎是使用C#所能够编写的最小的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/"); } } }

 

If you/'re not familiar with the STAThread attribute, it/'s a signal to .NET that when COM is initialized on the application/'s main thread, to make sure it/'s initialized to be compatible with single-threaded UI work , as required by WPF applications.

----------------------------------------------------------------

STAThread特性是当应用程序主线程完成COM组件的初始化时发送给.Net的信号,它用于确保COM组件已经完成初始化,能够胜任单线程用户界面的工作.该特性是WPF应用程序必须的.

 

1.1.1. Building Applications

Building this application is a matter of firing off the C# compiler from a command shell with the appropriate environment variables,[*] as in Example 1-2.

[*] Start --> Programs --> Microsoft WinFX SDK Debug Build Environment or Release Build Environment.

--------------------------------------------------------------

1.1.1.构建应用程序

可以通过在命令行模式中配置使用适当的环境变量并为C#编译器提供足够的信息来构建这个应用程序.

 

Example 1-2. Building a WPF application manuallyC://1st>csc /target:winexe /out:.//1st.exe /r:System.dll /r:c://WINDOWS//Microsoft.NET//Windows//v6.0.4030//WindowsBase .dll /r:c://WINDOWS//Microsoft.NET//Windows//v6.0.4030//PresentationCore .dll /r:c://WINDOWS//Microsoft.NET//Windows//v6.0.4030//PresentationFramework .dll MyApp.cs Microsoft (R) Visual C# 2005 Compiler version 8.00.50215.44 for Microsoft (R) Windows (R) 2005 Framework version 2.0.50215 Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

Here, we/'re telling the C# compiler that we/'d like to create a Windows application (instead of a Console application, which we get by default), putting the result, 1st.exe, into the current folder, bringing in the three main WPF assemblies (WindowsBase, PresentationCore and PresentationFramework), along with the core .NET System assembly, and compiling the MyApp.cs source file.

Running the resulting 1st.exe produces the world/'s lamest WPF application, as shown in Figure 1-1.

----------------------------------------------------------------

这里,我们介绍了使用C#编译器创建一个Windows应用程序(而不是默认的命令行应用程序),将编译的结果——1st.exe放入到当前目录下,在核心的.Net系统程序集之外加入三个主要的WPF程序集(WindowsBase, PresentationCore以及PresentationFramework),并编译MyApp.cs代码文件.

通过运行该程序,我们将看到这个最为简陋的WPF应用程序,如Figure 1-1所示.

 

Figure 1-1. A lame WPF application

 

In anticipation of less lame WPF applications, refactoring the compilation command line into an msbuild project file is recommended, as in Example 1-3.

-------------------------------------------------

为了减少不可靠的WPF应用程序,可以将编译命令行分解到一个msbuild工程文件中,如例1-3所示.

 

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>

Msbuild is a .NET 2.0 command-line tool that understands XML files in the form shown in Example 1-3. The file format is shared between msbuild and Visual Studio 2005 so that you can use the same project files for both command-line and IDE builds. In this .csproj file (which stands for /"C# Project/"), we/'re saying the same things that we said to the C# compileri.e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值