如何将 XAML 转换注入到我的构建中? [英] How can I inject a XAML transformation into my build?

查看:41
本文介绍了如何将 XAML 转换注入到我的构建中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想针对我的 XAML 资源词典运行自定义 EXE.假设我拥有的这个 exe 将删除注释、空格和未使用的资源.原始 XAML 文件需要保持不变,但最终出现在 XAP 和 DLL 中的 XAML (silverlight) 和 BAML (wpf) 需要转换.它需要在我的电脑和构建服务器上运行.

I'd like to run a custom EXE against my XAML Resource Dictionaries. Let's say this exe that I've got is going to strip out comments, whitespace and unused resources. The original XAML files need to be untouched, but the XAML (silverlight) and BAML (wpf) that ends up in the XAPs and DLLs needs to be transformed. It needs to work on my computer and the build server.

我的问题是:运行这个 exe 的最简单和最可靠的方法是什么?

My question is: what's the simplest and most reliable way to run this exe?

我的第一个想法是举办一个预构建活动.但这必须适用于原始 XAML 文件.开发会变得非常痛苦.

My first thought was to have a pre-build event. But this would have to work on the original XAML file. Development would become quite painful.

到 post build 事件运行时,我的资源已经编译到 dll 中.

By the time the post build event has been run, my resources are already compiled into the dlls.

我有哪些选择?

推荐答案

您应该将exe"实现为 MSbuild 任务.

You should implement the "exe" as an MSbuild Task.

本质上,您构建了一个继承自 Microsoft.Build.Utilities.Task 类的 C# 类,并覆盖了 Execute() 方法.

Essentially you build a C# class that inherits from the Microsoft.Build.Utilities.Task class, and overide the Execute() method.

类似的东西

public class CleanXAML : Task 
{
}

然后您指定(在您的构建文件或您导入的外部 .tasks 文件中,任务名称和您刚刚构建的 DLL 的路径)

Then you spefify (either in your build file, or an external .tasks file that you import, the task name and the path to the DLL you just built)

<UsingTask AssemblyFile="C:\customtasks\XamlTasks.dll"
   TaskName="Rob.CustomTasks.Xaml.CleanXaml"/>

这使您可以像调用任何其他 MsBuild 任务一样调用它

That enables you to invoke this like any other MsBuild task

<CleanXaml Source="$(PathtoOriginalXaml)" 
   Destination="$(SourceCodePath)\$(cleanXaml.xaml)" />

从那里您需要找出将其注入"到您的构建过程中的最佳方法.根据您的构建方式(msbuild、vs2010、teambuild 或 teambuild 工作流程),有不同的方法可以做到这一点.本质上,您需要在调用 CoreCompile 目标之前执行此操作,并确保您的输出 xaml"正确替换了 CSC.exe 预期的内容.

From there you need to figure out the best way to "inject" this into your build process. Depending on how you are building (msbuild, vs2010, teambuild, or teambuild workflow) there are different ways to do this. Essentially you need this to happen BEFORE the CoreCompile target is invoked and make sure your "output xaml" properly replaces what CSC.exe is going to expect.

对 MsBuild 任务进行一些搜索以获取更多信息,或提出您在此处遇到的任何问题.

Do some searches on MsBuild tasks for more info, or ask any question you've got here.

我强烈推荐这种方法与在 MSBuild 中使用 CallEXE 任务相比,b/c 这样 MSBuild 属性和项目保持在上下文中,因此您只是从构建步骤移动到构建步骤,而不是回避整个事情转换,然后希望它继续工作.

I would highly recommend this approach vs. using a CallEXE task in MSBuild, b/c this way the MSBuild properties and items stay in context so you are just moving from build step to build step, vs sidetracking the whole thing to do the transform, then hoping it keeps working.

这篇关于如何将 XAML 转换注入到我的构建中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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