13

I need to compile simple WPF application, which I have written using Visual Studio, with the command line C# compiler (i.e 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?

wonea
  • 4,783
  • 17
  • 86
  • 139
Andrew Lawmaking
  • 281
  • 3
  • 11
  • 5
    The compiler is very rarely wrong. I'd worry about getting the app to compile before you worry about running it from the command line. Without seeing your code, its hard to help tell you where the `InitializeComponent` call is supposed to go. Have you tried starting a new, blank WPF project, compiling it, and seeing what the differences are? – Ron Beyer Nov 02 '15 at 21:45
  • Thats a problem, even with new blank Wpf without any additional code compiler worry! – Andrew Lawmaking Nov 02 '15 at 21:50
  • Can you post the exact error message along with the line (and surrounding lines) that it says the error is occurring on? – Ron Beyer Nov 02 '15 at 21:51
  • I write in command line: csc /r:System.xaml.dll;System.dll...(and others dll which need for projects) *cs. Compiler says error CS0103: Name "InitializeComponent" does not exist in current context – Andrew Lawmaking Nov 02 '15 at 21:59
  • Why are you compiling from the command line? Are you sure you are specifying all the files, each WPF application includes a few files. – Ron Beyer Nov 02 '15 at 22:01
  • Its just curiosity! I thought that *.cs include all the files – Andrew Lawmaking Nov 02 '15 at 22:07
  • 1
    Looks like title is misleading. I read it first and thought you just wanted to "launch" application. – Nikhil Vartak Nov 03 '15 at 03:30
  • 3
    The `InitializeComponent()` method is not in `System.Xaml.dll`. It's in an auto-generated C# file that's part of your project output (e.g. `MainWindow.g.i.cs` by default). See also [Building a WPF Application (WPF)](https://msdn.microsoft.com/en-us/library/aa970678(v=vs.100).aspx). – Peter Duniho Nov 03 '15 at 05:25
  • 2
    Have you tried msbuild.exe? E.g.: `msbuild *.csproj /t:Build` – Mark Toman Nov 03 '15 at 05:54
  • additional opinion: You have changed the name of one of the windows from solution explorer but have not changed within .cs file. – SophisticatedUndoing Nov 03 '15 at 06:55
  • Thanks everybody! Yes, it works with msbuild but with main file projectname.csproj! Is it possible to launch wpf app with command line only using different parts of project: for example if i have App.Xaml.cs and MainWindow.xaml.cs? – Andrew Lawmaking Nov 03 '15 at 17:48
  • @wonea - Why a bounty on a old question ? Is there still an issue in building from command line ? – Subbu Sep 07 '17 at 02:38
  • @Subbu The question had no official answers, that's all. – wonea Sep 07 '17 at 07:39
  • you dont need to compile with command line, you need to fix your code – Fredrik Sep 07 '17 at 09:49
  • https://msdn.microsoft.com/en-us/library/aa970678(v=vs.85).aspx. This clearly states that WPF application is not supported by csc.exe – Ramankingdom Sep 11 '17 at 06:53
  • 1
    Seems like a duplicate question https://stackoverflow.com/questions/22316544/windows-how-to-compile-wpf-xaml-based-program-from-command-line – Didgeridoo Sep 12 '17 at 14:04

3 Answers3

9

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.

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
5

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

Zozo
  • 870
  • 1
  • 8
  • 27
1

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.

RJ Thompson
  • 126
  • 7