简体   繁体   中英

How to compile WPF program with command line?

I need to compile simple WPF application, which I have written using Visual Studio, with the command line C# compiler (ie csc.exe ).

Problem is the error CS0103 — the compiler says that there is no InitializeComponent() method in my program. But that's wrong, because I add System.Xaml.dll. Does anybody know how to solve this?

Building with MSBuild will be the easier path than with csc.exe. Get the path to the correct MSBuild ( Path to MSBuild ), since you can have multiple MSBuild versions on your computer, most of the time one per Visual Studio installation.

Then, simply build your project file:

msbuild YourProject.csproj /p:Configuration=Release

and the output will be in the bin/Release folder.

Building a WPF Application using Command Line Compilation A WPF application written entirely in code (ie no markup) can be built by using a command line compiler. For example, consider a WPF standalone application, written in C#, with the following source code files:

  • An application definition file (app.cs).
  • List item

A window (mainwindow.cs).

This application can be built using the C# command line compiler, csc.exe, using the following command:

 csc.exe /out:WPFApplication.exe /target:winexe app.cs mainwindow.cs /reference:"C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0\\presentationframework.dll" /reference:"C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0\\windowsbase.dll" /reference:"C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0\\presentationcore.dll" 

In this example:

  • The /out parameter is used to specify the name of compiled executable application (WPFApplication.exe).
  • The /target parameter is used to specify the type of application that is compiled (a Microsoft Windows executable).
  • The C# source code files are specified (app.cs and mainwindow.cs)
  • The /reference parameter is used to identify the .NET Framework 3.0 assemblies with types that are used from the source code.

While this application is composed of two source code files, command line compilation can be used to build applications with more complexity (see Building from the Command Line (CSharp)). However, command line compilation does not support the compilation of WPF applications that include Extensible Application Markup Language (XAML) markup source code files. Furthermore, command-line compilation is not robust enough to support the full range of build requirements that are required by typical WPF applications, including configuration management and ClickOnce application and deployment manifest generation. To support the more complex build requirements of WPF applications, WPF integrates with and extends the MSBuild.

Please note, that MSBuild might be better option for building more complex solutions, and it is supported out of box by most of the continuous integration solutions.

More info here: https://msdn.microsoft.com/en-us/library/aa970678(v=vs.85).aspx

If you don't mind the dev environment opening a sure fire way to compile is to use:

C:> devenv HacksWinSample.sln /build release

see http://archive.oreilly.com/pub/h/5186

Using this approach you can easily see what any problem is and debug it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM