Translate

Friday, 21 June 2013

Start with WPF

Window Presentation Format (WPF)


WPF is a completely new presentation framework, integrating the capabilities of
many frameworks that have come before it, including User, GDI, GDI+, and HTML,
as well as being heavily influenced by toolkits targeted at the Web, such as Adobe
Flash, and popular Windows applications like Microsoft Word. This chapter will
give you the basics of WPF from scratch, and then a whirlwind tour of the things
you’ll read about in detail in this Blog.

Building Applications In WPF


Building this application (Example 1-2) is a matter of firing off the C# compiler from
a command shell with the appropriate environment variables.* (The command line
here has been spread across multiple lines for readability, but you need to put it all
on one line.)

Example 1. Building a WPF application manually
C:\1st> csc /target:winexe /out:.\1st.exe
/r:System.dll
/r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll"
/r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationCore.dll"
/r:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\
PresentationFramework.dll"
MyApp.cs

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, referencing 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.

Example 2. 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>

The msbuild tool is a .NET 2.0 command-line application that understands XML
files in the form shown in Example 2. The file format is shared between msbuild
and Visual Studio 2005 so that you can use the same project files for both commandline
and integrated development environment (IDE) builds. In this .csproj file (which
stands for “C# Project”), we’re saying the same things we said to the C# compiler—
in other words, we’d like a Windows application, we’d like the output to be 1st.exe
in the current folder, and we’d like to reference the System assembly and the main
WPF assemblies while compiling the MyApp.cs file. The actual smarts of how to turn
these minimal settings into a compiled .NET application are contained in the .NET

2.0 Microsoft.CSharp.targets file that’s imported at the bottom of the file.


Example 3. Building using msbuild (continued)
Target CoreCompile:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\Csc.exe /noconfig /nowarn:1701
,1702 /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0
\PresentationCore.dll" /reference:"C:\Program Files\Reference Assemblies\Microso
ft\Framework\v3.0\PresentationFramework.dll" /reference:C:\Windows\Microsoft.NET
\Framework\v2.0.50727\System.dll /reference:"C:\Program Files\Reference Assembli
es\Microsoft\Framework\v3.0\WindowsBase.dll" /debug+ /out:obj\Debug\1st.exe /tar
get:winexe MyApp.cs
Target _CopyFilesMarkedCopyLocal:
Copying file from "C:\Program Files\Reference Assemblies\Microsoft\Framework
\v3.0\PresentationCore.dll" to ".\PresentationCore.dll".
Copying file from "C:\Program Files\Reference Assemblies\Microsoft\Framework
\v3.0\System.Printing.dll" to ".\System.Printing.dll".
Copying file from "C:\Program Files\Reference Assemblies\Microsoft\Framework
\v3.0\PresentationCore.xml" to ".\PresentationCore.xml".
Copying file from "C:\Program Files\Reference Assemblies\Microsoft\Framework
\v3.0\System.Printing.xml" to ".\System.Printing.xml".
Target CopyFilesToOutputDirectory:
Copying file from "obj\Debug\1st.exe" to ".\1st.exe".
1st -> C:\1st\1st.exe
Copying file from "obj\Debug\1st.pdb" to ".\1st.pdb".
Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:04.15

WPF Applications

A real WPF application is going to need more than a message box. WPF applications
have an instance of the Application class from the System.Windows namespace.
The Application class provides methods like Run for starting the application, events
like Startup and SessionEnding for tracking lifetime, and properties like Current,
ShutdownMode, and MainWindow for finding the global application object, choosing
when it shuts down, and getting the application’s main window. Typically, the

Application class serves as a base for custom application-wide data and behavior.

No comments: