1

我有一行代码是从一个名为Zetcode的网站获得的,为了构建第一个示例,我将代码编译如下:

gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll 01-simple-cs-example.cs -out:simple-sample.exe

这构建了绘制我的窗口的 exe,但我认为我的csproj文件不正确。

<Project DefaultTargest="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <!-- This Property will make a variable named MSBuildSample, then you can use it to 
            assign to whatever in the script.
            Properties are variables inside the script.
        -->
        <SrcPath>src\</SrcPath>
        <AssemblyName>01-simple-cs-example</AssemblyName>
        <OutputPath>bin\</OutputPath>
    </PropertyGroup>

    <ItemGroup>
        <!-- This Compile statement, gathers all the source files listed in an item called Compiled 
            ItemGroups interact with the source code.
        -->
        <Compile Include="$(SrcPath)01-simple-cs-example.cs" />
    </ItemGroup>
    <!-- In the Build target, the Inputs and Outputs targets which looks to see if the files have been updated,
        or if the files are existent. If the files have not been changed, then the Build target is skipped. 
    -->
    <Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe" >
        <!-- The MakeDir directive will create the directory in the property group above, if it meets the 
            condition stated in the Condition= statement. 
        -->
        <Message Text="Creating the output path $(OutputPath)." />
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
        <!-- This Csc is the .NET C# compiler, which then uses the ItemGroup of collected sources called, Compile 
        -->
        <Message Text="Compiling the source code." />
        <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
    </Target>



</Project>

这是尝试运行 xbuild 和 msbuild 的结果。

C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example>xbuild
XBuild Engine Version 3.3.0.0
Mono, Version 3.3.0.0
Copyright (C) Marek Sieradzki 2005-2008, Novell 2008-2011.

Build started 7/25/2014 2:47:41 PM.
__________________________________________________
Project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj" (default target(s)):
        Target Build:
                Created directory "bin\"
                Tool C:\mono\Mono-3.2.3\bin\gmcs.bat execution started with arguments:  /out:bin\01-simple-cs-example.exe src\01-simple-cs-example.cs
src\01-simple-cs-example.cs(1,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
src\01-simple-cs-example.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
src\01-simple-cs-example.cs(4,23): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
        Task "Csc" execution -- FAILED
        Done building target "Build" in project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj".-- FAILED
Done building project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj".-- FAILED

Build FAILED.
Errors:

C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj (default targets) ->
(Build target) ->

        src\01-simple-cs-example.cs(1,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
        src\01-simple-cs-example.cs(2,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing `System.Drawing' assembly reference?
        src\01-simple-cs-example.cs(4,23): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?

         0 Warning(s)
         3 Error(s)

Time Elapsed 00:00:01.5341534

这是 MSBuild 运行的结果。MSBuild 成功完成,但我想这是因为 MSBuild 知道在哪里查看,而无需我在csproj文件中指定它。

C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example>MSBuild
Microsoft (R) Build Engine version 4.0.30319.18408
[Microsoft .NET Framework, version 4.0.30319.18444]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 7/25/2014 2:48:04 PM.
Project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj" on node 1 (default targets).
Build:
  C:\Windows\Microsoft.NET\Framework\v2.0.50727\Csc.exe /out:bin\01-simple-cs-example.exe src\01-simple-cs-example.cs
Done Building Project "C:\Users\User01\Dropbox\programming\csharp\zetcode-csharp-winforms\01-simple-cs-example\01-simple-cs-example.csproj" (default targets).


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

Time Elapsed 00:00:04.36

我不知道要在csproj文件中放入什么以使其加载我在命令行中使用的那些 DLL 以使用gmcs. 有什么线索吗?

-- 编辑 2014-08-20 --

在使用knocte的答案后,我能够看到它们是如何References添加到csproj文件中的。它就像添加一个项目组一样简单,然后添加您using在 C# 源文件中列出的引用。

  <ItemGroup>
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Drawing" />
    <Reference Include="System" />
  </ItemGroup>

如果您手动构建csproj文件,这是手动方式。

4

1 回答 1

-1

只需使用 IDE(MonoDevelop 或 Visual Studio)向库添加 GACSystem.Windows.Forms引用System.Drawing

于 2014-08-20T09:24:57.473 回答