visual-studio Visual Studio 项目属性中的各种“构建操作”设置是什么?它们的作用是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/145752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 09:40:32  来源:igfitidea点击:

What are the various "Build action" settings in Visual Studio project properties and what do they do?

visual-studioprojects-and-solutions

提问by Gishu

For the most part, you just take whatever Visual Studio sets it for you as a default... I'm referring to the BuildActionproperty for each file selected in Solution Explorer. There are a number of options and it's difficult to know what each one of them will do.

在大多数情况下,您只需将 Visual Studio 为您设置的任何内容作为默认值...我指的是解决方案资源管理器中选择的每个文件的BuildAction属性。有多种选择,很难知道每个选择会做什么。

回答by Gishu

  • None: The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.

  • Compile: The file is compiled into the build output. This setting is used for code files.

  • Content: Allows you to retrieve a file (in the same directory as the assembly) as a stream via Application.GetContentStream(URI). For this method to work, it needs a AssemblyAssociatedContentFile custom attribute which Visual Studio graciously adds when you mark a file as "Content"

  • Embedded resource: Embeds the file in an exclusive assembly manifest resource.

  • Resource(WPF only): Embeds the file in a shared (by all files in the assembly with similar setting) assembly manifest resource named AppName.g.resources.

  • Page(WPF only): Used to compile a xamlfile into baml. The bamlis then embedded with the same technique as Resource(i.e. available as `AppName.g.resources)

  • ApplicationDefinition(WPF only): Mark the XAML/class file that defines your application. You specify the code-behind with the x:Class="Namespace.ClassName" and set the startup form/page with StartupUri="Window1.xaml"

  • SplashScreen(WPF only): An image that is marked as SplashScreenis shown automatically when an WPF application loads, and then fades

  • DesignData: Compiles XAML viewmodels so that usercontrols can be previewed with sample data in Visual Studio (uses mock types)

  • DesignDataWithDesignTimeCreatableTypes: Compiles XAML viewmodels so that usercontrols can be previewed with sample data in Visual Studio (uses actual types)

  • EntityDeploy: (Entity Framework): used to deploy the Entity Framework artifacts

  • CodeAnalysisDictionary: An XML file containing custom word dictionaryfor spelling rules

  • None:该文件不包含在项目输出组中,也不会在构建过程中编译。一个示例是包含文档的文本文件,例如自述文件。

  • 编译:文件被编译到构建输出中。此设置用于代码文件。

  • Content:允许您通过 Application.GetContentStream(URI) 以流的形式检索文件(在与程序集相同的目录中)。要使此方法起作用,它需要一个 AssemblyAssociatedContentFile 自定义属性,当您将文件标记为“内容”时,Visual Studio 会慷慨地添加该属性

  • 嵌入资源:将文件嵌入到独占程序集清单资源中。

  • 资源(仅限 WPF):将文件嵌入名为 AppName.g.resources 的共享(程序集中的所有文件具有类似设置)程序集清单资源中。

  • Page (仅限 WPF):用于将xaml文件编译为baml. 所述baml然后嵌有相同的技术Resource(即,可作为`AppName.g.resources)

  • ApplicationDefinition (仅限 WPF):标记定义应用程序的 XAML/类文件。您使用 x:Class="Namespace.ClassName" 指定代码隐藏并使用 StartupUri="Window1.xaml" 设置启动表单/页面

  • SplashScreen (仅限 WPF):标记为在 WPF 应用程序加载时自动显示的图像,SplashScreen然后淡出

  • DesignData:编译 XAML 视图模型,以便可以在 Visual Studio 中使用示例数据预览用户控件(使用模拟类型)

  • DesignDataWithDesignTimeCreatableTypes:编译 XAML 视图模型,以便可以在 Visual Studio 中使用示例数据预览用户控件(使用实际类型)

  • EntityDeploy: (Entity Framework):用于部署实体框架工件

  • CodeAnalysisDictionary:包含用于拼写规则的自定义单词词典的 XML 文件

回答by Paul Batum

From the documentation:

从文档:

The BuildAction property indicates what Visual Studio does with a file when a build is executed. BuildAction can have one of several values:

None - The file is not included in the project output group and is not compiled in the build process. An example is a text file that contains documentation, such as a Readme file.

Compile - The file is compiled into the build output. This setting is used for code files.

Content - The file is not compiled, but is included in the Content output group. For example, this setting is the default value for an .htm or other kind of Web file.

Embedded Resource - This file is embedded in the main project build output as a DLL or executable. It is typically used for resource files.

BuildAction 属性指示 Visual Studio 在执行构建时对文件执行的操作。BuildAction 可以具有以下几个值之一:

无 - 该文件不包含在项目输出组中,也不会在构建过程中编译。一个示例是包含文档的文本文件,例如自述文件。

编译 - 文件被编译到构建输出中。此设置用于代码文件。

内容 - 文件未编译,但包含在内容输出组中。例如,此设置是 .htm 或其他类型的 Web 文件的默认值。

嵌入资源 - 此文件作为 DLL 或可执行文件嵌入在主项目构建输出中。它通常用于资源文件。

回答by Dominic Hopton

Page -- Takes the specified XAML file, and compiles into BAML, and embeds that output into the managed resource stream for your assembly (specifically AssemblyName.g.resources), Additionally, if you have the appropriate attributes on the root XAML element in the file, it will create a blah.g.cs file, which will contain a partial class of the "codebehind" for that page; this basically involves a call to the BAML goop to re-hydrate the file into memory, and to set any of the member variables of your class to the now-created items (e.g. if you put x:Name="foo" on an item, you'll be able to do this.foo.Background = Purple; or similar.

Page -- 获取指定的 XAML 文件,并编译为 BAML,并将该输出嵌入到程序集的托管资源流中(特别是 AssemblyName.g.resources),此外,如果您在根 XAML 元素上具有适当的属性文件,它将创建一个 blah.g.cs 文件,该文件将包含该页面的“代码隐藏”的部分类;这基本上涉及调用 BAML goop 将文件重新水合到内存中,并将类的任何成员变量设置为现在创建的项目(例如,如果您将 x:Name="foo" 放在项目上,你将能够做到 this.foo.Background = Purple; 或类似的。

ApplicationDefinition -- similar to Page, except it goes onestep furthur, and defines the entry point for your application that will instantiate your app object, call run on it, which will then instantiate the type set by the StartupUri property, and will give your mainwindow.

ApplicationDefinition -- 类似于 Page,除了它更进一步,并定义应用程序的入口点,该入口点将实例化您的应用程序对象,在其上调用 run,然后实例化由 StartupUri 属性设置的类型,并将提供您的主窗口.

Also, to be clear, this question overall is infinate in it's results set; anyone can define additional BuildActions just by building an MSBuild Task. If you look in the %systemroot%\Microsoft.net\framework\v{version}\ directory, and look at the Microsoft.Common.targets file, you should be able to decipher many more (example, with VS Pro and above, there is a "Shadow" action that allows you generate private accessors to help with unit testing private classes.

此外,需要明确的是,这个问题在其结果集中总体上是无限的;任何人都可以通过构建 MSBuild 任务来定义额外的 BuildAction。如果您查看 %systemroot%\Microsoft.net\framework\v{version}\ 目录,并查看 Microsoft.Common.targets 文件,您应该能够破译更多(例如,使用 VS Pro 及更高版本,有一个“Shadow”操作允许您生成私有访问器来帮助单元测试私有类。

回答by Gerard ONeill

VS2010 has a property for 'Build Action', and also for 'Copy to Output Directory'. So an action of 'None' will still copy over to the build directory if the copy property is set to 'Copy if Newer' or 'Copy Always'.

VS2010 有一个用于“构建操作”和“复制到输出目录”的属性。因此,如果复制属性设置为“如果较新则复制”或“始终复制”,“无”操作仍将复制到构建目录。

So a Build Action of 'Content' should be reserved to indicate content you will access via 'Application.GetContentStream'

因此,应保留“内容”的构建操作以指示您将通过“Application.GetContentStream”访问的内容

I used the 'Build Action' setting of 'None' and the 'Copy to Output Direcotry' setting of 'Copy if Newer' for some externally linked .config includes.

对于某些外部链接的 .config 包含,我使用了“无”的“构建操作”设置和“如果较新则复制”的“复制到输出目录”设置。

G.

G。

回答by James Moore

In VS2008, the doc entry that seems the most useful is:

在 VS2008 中,似乎最有用的文档条目是:

Windows Presentation Foundation Building a WPF Application (WPF)

Windows Presentation Foundation 构建 WPF 应用程序 (WPF)

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/wpf_conceptual/html/a58696fd-bdad-4b55-9759-136dfdf8b91c.htm

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/wpf_conceptual/html/a58696fd-bdad-4b55-9759-136dfdf8b91c.htm

ApplicationDefinition Identifies the XAML markup file that contains the application definition (a XAML markup file whose root element is Application). ApplicationDefinition is mandatory when Install is true and OutputType is winexe. A WPF application and, consequently, an MSBuild project can only have one ApplicationDefinition.

ApplicationDefinition 标识包含应用程序定义的 XAML 标记文件(根元素为 Application 的 XAML 标记文件)。当 Install 为 true 且 OutputType 为 winexe 时,ApplicationDefinition 是必需的。一个 WPF 应用程序以及一个 MSBuild 项目只能有一个 ApplicationDefinition。

Page Identifies a XAML markup file whose content is converted to a binary format and compiled into an assembly. Page items are typically implemented in conjunction with a code-behind class.

页面标识 XAML 标记文件,其内容已转换为二进制格式并编译为程序集。页面项通常与代码隐藏类一起实现。

The most common Page items are XAML files whose top-level elements are one of the following:

最常见的页面项目是 XAML 文件,其顶级元素是以下之一:

Window (System.Windows..::.Window).

Page (System.Windows.Controls..::.Page).

PageFunction (System.Windows.Navigation..::.PageFunction<(Of <(T>)>)).

ResourceDictionary (System.Windows..::.ResourceDictionary).

FlowDocument (System.Windows.Documents..::.FlowDocument).

UserControl (System.Windows.Controls..::.UserControl).

Resource Identifies a resource file that is compiled into an application assembly. As mentioned earlier, UICulture processes Resource items.

Resource 标识编译到应用程序程序集中的资源文件。如前所述,UICulture 处理资源项。

Content Identifies a content file that is distributed with an application. Metadata that describes the content file is compiled into the application (using AssemblyAssociatedContentFileAttribute).

内容 标识随应用程序分发的内容文件。描述内容文件的元数据被编译到应用程序中(使用 AssemblyAssociatedContentFileAttribute)。

回答by Per Lundberg

How about this pagefrom Microsoft Connect (explaining the DesignData and DesignDataWithDesignTimeCreatableTypes) types. Quoting:

这个来自 Microsoft Connect(解释 DesignData 和 DesignDataWithDesignTimeCreatableTypes)类型的页面怎么样。引用:

The following describes the two Build Actions for Sample Data files.

下面描述了示例数据文件的两个构建操作。

Sample data .xaml files must be assigned one of the below Build Actions:

必须为示例数据 .xaml 文件分配以下构建操作之一:

DesignData: Sample data types will be created as faux types. Use this Build Action when the sample data types are not creatable or have read-only properties that you want to defined sample data values for.

DesignData示例数据类型将创建为虚假类型。当示例数据类型不可创建或具有要为其定义示例数据值的只读属性时,请使用此构建操作。

DesignDataWithDesignTimeCreatableTypes: Sample data types will be created using the types defined in the sample data file. Use this Build Action when the sample data types are creatable using their default empty constructor.

DesignDataWithDesignTimeCreatableTypes将使用示例数据文件中定义的类型创建示例数据类型。当示例数据类型可使用其默认的空构造函数创建时,使用此构建操作。

Not soincredibly exhaustive, but it at least gives a hint. This MSDN walkthroughalso gives some ideas. I don't know whether these Build Actions are applicable for non-Silverlight projects also.

不是那么详尽,但它至少给出了一个提示。这个 MSDN 演练也给出了一些想法。我不知道这些构建操作是否也适用于非 Silverlight 项目。

回答by Jorge Garcia

  • Fakes:Part of the Microsoft Fakes (Unit Test Isolation) Framework. Not available on all Visual Studio versions. Fakes are used to support unit testing in your project, helping you isolate the code you are testing by replacing other parts of the application with stubs or shims. More here: https://msdn.microsoft.com/en-us/library/hh549175.aspx
  • Fakes:Microsoft Fakes(单元测试隔离)框架的一部分。并非在所有 Visual Studio 版本上都可用。Fakes 用于支持项目中的单元测试,通过用存根或填充程序替换应用程序的其他部分来帮助您隔离正在测试的代码。更多信息:https: //msdn.microsoft.com/en-us/library/hh549175.aspx