Building a Longhorn Executable Application


Here s a simple, but relatively comprehensive, project file that builds an executable Longhorn application:

 <Project DefaultTargets="Build"> 
<PropertyGroup>
<Property Language="C#" />
<Property DefaultClrNameSpace="IntroLonghorn" />
<Property TargetName="MyApp" />
</PropertyGroup>

<Import Project="$(LAPI)\WindowsApplication.target" />

<ItemGroup>
<Item Type="ApplicationDefinition" Include="MyApp.xaml" />

<Item Type="Pages" Include="HomePage.xaml" />
<Item Type="Pages" Include="DetailPage.xaml" />
<Item Type="Code" Include="DetailPage.xaml.cs"/>

<Item Type="DependentProjects" Include="MyDependentAssembly.proj" />

<Item Type="Components" Include="SomeThirdParty.dll" />

<Item Type="Resources" Include="Picture1.jpg"
FileStorage="embedded" Localizable="False"/>
<Item Type="Resources" Include="Picture2.jpg"
FileStorage="embedded" Localizable="True"/>
</ItemGroup>
</Project>

The Project Element

All project files begin with a root element definition named Project . Its DefaultTargets attribute specifies the names of the targets that the system should build when you don t otherwise specify a target. In this example, I specify that, by default, the system should build the target named Build .

The PropertyGroup and Property Elements

Build rules can conditionally execute based on property values. As mentioned, a property s value can originate from an environment variable, from an MSBuild command-line switch, or from a property definition in a project file.

A project for an application must specify, at a minimum, a value for the Language and TargetName properties. In this example, I specify that the language is C# and that the name of the resulting application should be MyApp . I ve also assigned a value to the property named DefaultClrNameSpace .

The build system compiles each XAML file into a managed class definition. By default, the managed class will have the same name as the base file name of the XAML source file. For example, the file Markup.xaml compiles into a definition of a class named Markup . By setting the DefaultClrNameSpace property to IntroLonghorn , I m asking the build system to prefix generated class names with the IntroLonghorn namespace. Because of this, the build system produces a class named IntroLonghorn.Markup for the Markup.xaml definition.

I defined my properties prior to importing other projects, so the rules in the imported projects will use my specified property values ”for example, I ll get the proper build rules for C# applications because I define the Language property as C# .

The Import Element

The rules in the Build target produce my Longhorn application s executable file. Specifying those build rules in every project file would be tedious and repetitive. So a little later in the project file, I use the following definition to import a predefined project file named WindowsApplication.target :

 <Import Project="$(LAPI)\WindowsApplication.target" /> 

This imported file contains the standard build rules for building a Windows application, and it (indirectly) defines the target named Build .

The ItemGroup and Item Elements

The ItemGroup element and its child Item elements define all the parts required to build the application.

You must have one Item with a Type of ApplicationDefinition , as shown here. This Item specifies the file that describes the Application object to use for your application. The Application object is typically an instance of either the MSAvalon.Windows.Application class or the MSAvalon.Windows.Navigation.NavigationApplication class, both of which I describe later in this chapter.

 <Item Type="ApplicationDefinition Include="MyApp.xaml /> 

Each Item with a Type of Pages defines a set of XAML files, as shown here. The build system compiles these XAML definitions into classes that it includes in the resulting assembly.

 <Item Type="Pages" Include="HomePage.xaml" /> 
<Item Type="Pages" Include="DetailPage.xaml" />

Each Item with a Type of Code represents a source file, as shown here. The build system compiles these source files using the appropriate compiler selected by your project s Language property.

 <Item Type=Code Include=DetailPage.xaml.cs/> 

This project might depend on other projects. The build system must compile these dependent projects before it can build this project. You list each such dependent project using an Item with Type of DependentProjects :

 <Item Type="DependentProjects Include=MyDependentAssembly.proj /> 

Code in this project might use types in a prebuilt assembly, also known as a component assembly . To compile code using such component assemblies, the compiler needs a reference to each assembly. In addition, when you deploy your application, you will need to deploy these component assemblies as well. You list each component assembly using an Item with Type of Components :

 <Item Type="Components Include="SomeThirdParty.dll /> 

A referenced assembly is somewhat different from a component assembly. In both cases, your code uses types in a prebuilt assembly. However, you don t ship a referenced assembly as part of your application, whereas you do ship a component assembly as part of your application. The build system needs to know this distinction.

You specify an Item with a Type of References to indicate that the compiler must reference the specified assembly at build time, as shown here, but the assembly will not be part of the application deployment. The build system automatically includes references to standard system assemblies ”for example, mscorlib.dll, System.dll, PresentationFramework.dll. and more ”but you ll have to add any nonstandard assembly your application must reference.

 <Item Type="References Include="SharedThirdParty.dll /> 

Your application might also use resources. An Item with a Type of Resources describes a resource used by the application, as shown here. The build system can embed the resource into the resulting assembly or include it as a stand-alone file. The build system can also place localizable resources into satellite assemblies.

 <Item Type="Resources" Include="Picture1.jpg" 
FileStorage="embedded" Localizable="False"/>
<Item Type="Resources" Include="Picture2.jpg"
FileStorage="embedded" Localizable="True"/>



Introducing Microsoft WinFX
Introducing WinFX(TM) The Application Programming Interface for the Next Generation of Microsoft Windows Code Name Longhorn (Pro Developer)
ISBN: 0735620857
EAN: 2147483647
Year: 2004
Pages: 83
Authors: Brent Rector

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net