Custom build action properties in a Visual Studio C# project

*爱你&永不变心* 提交于 2020-01-15 07:15:52

问题


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. 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>

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

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?


回答1:


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

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

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.I have a special image resource 'compiler' the transform the image (this compiler is defined using a msbuild target)

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

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

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

Update:

  1. 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?

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.



来源:https://stackoverflow.com/questions/43283613/custom-build-action-properties-in-a-visual-studio-c-sharp-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!