我们的特定项目适用于 Windows Phone,但我认为这个问题同样适用于所有基于 xaml 的项目。我们有一堆仅在设计时使用的图像 Assets ,不希望它们作为已发布版本的一部分。如果可能,我们希望它们不属于 Debug
配置构建,要么。有没有人有关于如何去做的最佳实践?
一种解决方案是继续在项目中包含图像,但将构建操作设置为 None
.这样做的问题是很容易忘记,因为图像的默认值是将它们包含为 Content
.
理论上,另一种解决方案是将这些图像添加到仅在 Debug 版本中引用的程序集,但我还没有对其进行测试。我不知道 Blend 或 VS 设计器是否能很好地与该解决方案配合使用,无论如何我们宁愿不将图像作为 Debug 构建的一部分。
最佳答案
我们使用 DesignTime
数据相当多,并且确实将它们从发布版本中排除,
Maximizing the Visual Designer’s Usage with Design-Time Data在最底部有一个部分“从生产构建中排除设计时数据”,它可以很好地满足我们的要求。
摘自以上链接:
Excluding Design-Time Data from Production Builds When handled carefully, the design-time code isn’t harmful to production applications (because it isn’t executed at run time), but excluding this code and all the corresponding assets (such as images) from the production build is still preferable.
To do that, you need to modify the CSPROJ file manually with the following steps. There’s no other way to do this in the current version of Visual Studio 2012.
并选择重新加载项目。
<Compile Include="Design\DesignRssService.cs"
Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "/>
可以在所有设计时文件上设置此条件,包括图片和其他 Assets 。排除的项目在解决方案资源管理器中仍然可见,但它们不会包含在最终的二进制文件中。事实上,如果你在排除 DesignRssService 之后构建解决方案,你会在 ViewModelLocator 中得到一个编译错误。
您必须将使用 DesignRssService 的代码行包含在预编译器指令中,如下所示:
if (ViewModelBase.IsInDesignModeStatic)
{
#if DEBUG
SimpleIoc.Default.Register<IRssService, Design.DesignRssService>();
#endif
}
else
{
SimpleIoc.Default.Register<IRssService, RssService>();
}
关于wpf - 将设计时资源排除在构建之外的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574648/