Compiling WPF Applications with MSBuild

A tutorial example is provided on how to compile a WPF application with MSBuild tool and a project file. It helps to specify reference assembly files.

Compiling a WPF application with "csc" at the command line requires us to write a very long command as shown in the previous tutorial. We can make the compilation job easier if we use the MSBuild tool.

Here is the MSBuild project file to compile WpfButtonTest.cs given in the previous tutorial.

<!-- WpfButtonTest.proj
// Copyright (c) 2015 by Dr. Herong Yang, herongyang.com
-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
     <AssemblyName>WpfButtonTest</AssemblyName>
     <NET>C:\windows\Microsoft.NET\Framework\v4.0.30319</NET>
     <REF1>C:\Program Files\Reference Assemblies</REF1>
     <REF2>\Microsoft\Framework\.NETFramework\v4.6.1</REF2>
     <REF>$(REF1)$(REF2)</REF>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="WpfButtonTest.cs" />
    <Reference Include="$(REF)\PresentationFramework.dll" />
    <Reference Include="$(REF)\PresentationCore.dll" />
    <Reference Include="$(REF)\System.Xaml.dll" />
    <Reference Include="$(REF)\WindowsBase.dll" />
  </ItemGroup>
  <Target Name="Build">
    <Csc ToolPath="$(NET)" Sources="@(Compile)" 
       References="@(Reference)"
       OutputAssembly="$(AssemblyName).exe"/>  
  </Target>
</Project>

Run MSBuild with the above project file:

C:\herong>%NET%\msbuild WpfButtonTest.proj
Microsoft (R) Build Engine version 4.6.1055.0
[Microsoft .NET Framework, version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 9:23:35 PM.
Project "C:\herong\WpfButtonTest.proj" on node 1 (defau
lt targets).
Build:
C:\windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe 
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  \Framework\.NETFramework\v4.6.1\PresentationFramework.dll" 
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  \Framework\.NETFramework\v4.6.1\PresentationCore.dll" 
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  \Framework\.NETFramework\v4.6.1\System.Xaml.dll" 
  /reference:"C:\Program Files\Reference Assemblies\Microsoft
  \Framework\.NETFramework\v4.6.1\WindowsBase.dll" 
  /out:WpfButtonTest.exe WpfButtonTest.cs

Done Building Project "C:\herong\WpfButtonTest.proj" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.67

Very nice. The MSBuild tool created the final "csc" command with all pieces of information provided in the project file.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

WPF - Windows Presentation Foundation

 What Is WPF?

 System.Windows.Application Class

 System.Windows.Window Class

 HelloWPF.cs - First WPF Application

 System.Windows.Controls Namespace

 WPF Button Click Test

Compiling WPF Applications with MSBuild

 What Is XAML?

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version