by Keyvan Nayyeri via Keyvan Nayyeri on 11/22/2006 7:08:15 PM
Often building an application is as easy as a click Visual Studio but sometimes custom builds are required to create an application. If you install .NET Framework 3.0 and Windows Presentation Foundation add-in for Visual Studio 2005, can build your applications via Visual Studio.
But like all other .NET applications, it's possible to build your Windows Presentation Foundation applications using MSBuild. In this post I introduce this topic.
First I create a very simple Windows Presentation Foundation application which consists of one window and has following XAML code:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test Application" Height="180" Width="220"
>
<StackPanel Margin="60">
<Button Background="MediumSeaGreen">
Click!
</Button>
</StackPanel>
</Window>
In simplest form building a Windows Presentation Foundation application using MSBuild is nothing more than adding appropriate references and files as well as importing appropriate targets for WinFX and your desire language (C# in my case). Let's talk about these steps in my case.
First I start my MSBuild file:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
After this, like other MSBuild files, I use a <PropertyGroup /> element to set my assembly name (TestApplication), output type (winexe) and output path (current path):
<PropertyGroup>
<AssemblyName>TestApplication</AssemblyName>
<OutputType>winexe</OutputType>
<OutputPath>.\</OutputPath>
</PropertyGroup>
Now I add my references for this build. Note that I use all default references for a window but you can add or remove these references based on your needs.
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="UIAutomationProvider" />
<Reference Include="UIAutomationTypes" />
<Reference Include="ReachFramework" />
<Reference Include="System.Printing" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.IdentityModel" />
</ItemGroup>
Next step is to add build items. My application is simple and has only one XAML file. There is an application definition XAML file which should be presented in all Windows Presentation Foundation and is a necessary file for building applications. It can be used as a global file. For example you can define global resources in this file to set a constant look for your applications. Visual Studio automatically generates an App.xaml file for this purpose. I use same file name with following content for my application (it's similar to Visual Studio generated file with a minor change):
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
StartupUri="Window1.xaml"
<Application.Resources>
</Application.Resources>
</Application>
The code shown below is my MSBuild file after adding my build items:
<ApplicationDefinition Include="App.xaml" />
<Page Include="Window1.xaml" />
And finally I add targets for C# language and WinFX to let my build to find its default target. Final code is presented here:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
Now if I build my application using this MSBuild file and run it, get this result:
Now playing: Modern Talking - We Take The Chance
Original Post: Build Windows Presentation Foundation Applications Using MSBuild
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.