简体   繁体   中英

How can you use MSBuild to rename a source file before compilation?

I have a problem where I want to rename/move/copy a source XAML file before compiling it. I can see that there is a Move and a Copy task as a part of MSBuild that you can put into the .csproj file and it will do this. Unfortunately, I have only been able to get this to work with compiled files. Are either of these tasks supposed to work on the source files themselves?

If it is of any value, I'll elaborate a bit on what I'm trying to do, in case somebody has a completely different solution. Basically, our WPF application needs to be brandable. We've got the different brands in different XAML files (different icons and properties, etc.). These files are named something along the lines of BrandA.xaml, BrandB.xaml, etc. I'm currently trying to get exactly one of those brand files to be compiled into just simply Brand.xaml, which the rest of the application would then load. I've been able to get just a single file to copy over using MSBuild using conditions, but I haven't been able to get it named correctly.

Don't include the different brands as Page , but rather your custom item type, eg Brand (to ensure it's still part of the project and shows up in Visual Studio):

<Brand Include="BrandA.xaml">
   <Generator>MSBuild:Compile</Generator>
   <SubType>Designer</SubType>
</Brand>

You can then use the BeforeBuild target to copy the relevant file to Brand.xaml and include it in the Page item group. You should be able to use both SourceFiles and DestinationFiles to rename the file as part of the copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM