Visual Studio C#项目中的自定义生成操作属性 [英] Custom build action properties in a Visual Studio C# project

查看:374
本文介绍了Visual Studio C#项目中的自定义生成操作属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio C ++项目中,我可以定义自定义生成操作和相应的属性规则(请参阅MSBuild文件夹下的cl.xml).如何为C#项目中的自定义生成操作创建这样的规则?在MSBuild文件夹下,有一个文件Microsoft.CSharp.CurrentVersion.targets,它引用了CSharp.ProjectItemsSchema.xamlCSharp.xamlCSharp.BrowseObject.xaml,看起来非常像我需要的定义.使用procmon,我可以看到根本没有访问这些文件.定义自定义构建操作的正确方法是什么?

In a Visual Studio C++ project I can define custom build actions and corresponding property rules (see cl.xml under the MSBuild folder). How can I create such a rule for a custom build action in a C# project? Under the MSBuild folder there's a the file Microsoft.CSharp.CurrentVersion.targets which references CSharp.ProjectItemsSchema.xaml, CSharp.xaml and CSharp.BrowseObject.xaml which looks exacltly like the definitions I need. Using procmon I can see that these files are not accessed at all. What's the correct way to define custom build actions?

为了阐明我要完成的操作,下面是一个示例:

To clarify what I want to accomplish here's an example:

  1. 我将图像文件(.png)添加到项目中
  2. 我有一个特殊的图像资源编译器"来转换图像(此编译器是使用msbuild目标定义的)
  3. 我想更改图像文件的属性(例如目标分辨率,格式等).在C ++项目中,我可以为此打开文件属性对话框. 这些属性将另存为MSBuild项目元数据,并转发到MSBuild目标.
  1. I add an image file (.png) to the project
  2. I have a special image resource 'compiler' the transform the image (this compiler is defined using a msbuild target)
  3. I want to change properties of the image file (e.g. target resolution, format, etc.). In a C++ project I can open the file property dialog for this. These properties are saved as MSBuild item metadata and forwarded to the MSBuild target.

生成的项目文件将包含以下数据:

The resulting project file would contain data like this:

<ItemGroup>
    <MyImageContent Include="Image.png">
      <OutputName>MyImage.png</OutputName>
      <TargetResX>512</TargetResX>
      <TargetResY>256</TargetResY>
    </MyImageContent>
  </ItemGroup>

自定义MSBuild目标可以轻松使用此附加元数据.到目前为止,我知道该怎么做.

This additional metadata can easily be used by a custom MSBuild target. So far I know how to do it.

但是:在VC ++项目中,可以在Visual Studio提供的属性窗口中轻松编辑此元数据. Visual Studio使用的定义来自类似于我提到的cl.xml的rule xml文件.如何在C#项目中完成此任务?

But: In a VC++ project this metadata can be easily edited in a property window provided by Visual Studio. The definition Visual Studio uses comes from a rules xml file similar to the cl.xml I mentioned. How can I accomplish this in a C# project?

推荐答案

定义自定义构建操作的正确方法是什么?

What's the correct way to define custom build actions?

您可以使用 MSBuild目标,以在项目文件中定义自定义生成操作.

You can use the MSBuild Targets or imported .targets file to define custom build actions in the project file.

1.我将图像文件(.png)添加到项目中 对于此生成操作,您可以在目标中使用复制任务将该图像文件复制到项目文件夹:

1.I add an image file (.png) to the project For this build action, you can use a copy task in the target to copy the image file to the project folder:

    <Target Name="CopyFiles">  
    <Copy  
        SourceFiles="@(MySourceFiles)"  
        DestinationFolder="c:\MyProject\Destination"  
    />  
</Target> 

2.我有一个特殊的图像资源编译器"来转换图像(此编译器是使用msbuild目标定义的)

2.I have a special image resource 'compiler' the transform the image (this compiler is defined using a msbuild target)

对于此构建操作,您可以导入该msbuild目标.

For this build action, you can import that msbuild target.

3.我要更改图像文件的属性.

3.I want to change properties of the image file.

在C#项目中,图像的属性也保存为MSBuild项元数据,并转发到MSBuild目标.

In the C# project, the properties of the image are also saved as MSBuild item metadata and forwarded to the MSBuild target.

更新:

  1. 在VC ++项目中,可以在Visual Studio提供的属性窗口中轻松地编辑此元数据. Visual Studio使用的定义来自类似于我提到的cl.xml的rules xml文件.如何在C#项目中完成此任务?

据我所知,我们无法使自定义项具有其他元数据,这些元数据可以在属性窗口中进行编辑.这仅适用于C ++和C ++/CLI项目. .NET项目(C#和Visual Basic)没有太多要调整的选项.您可以参考在Visual C ++中共享项目属性了解更多信息.

As far as I know we could not make custom items have additional metadata which be editable in the property window. This only can be applied to C++ and C++/CLI projects. .NET projects (C# and Visual Basic) don’t have that many options to be tweaked. You can refer to the Sharing Project Properties in Visual C++ for more detail.

希望这可以为您提供帮助.

Hope this can help you.

这篇关于Visual Studio C#项目中的自定义生成操作属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆