wpf - 将设计时资源排除在构建之外的最佳方法是什么?

标签 wpf silverlight xaml windows-phone

我们的特定项目适用于 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.


  • 在解决方案资源管理器中右键单击要编辑的项目。
  • 从上下文菜单中,选择卸载项目。
  • 右键单击同一个项目,然后选择编辑 [项目名称]。
  • 找到要排除的文件,并为每个文件向 CSPROJ 文件中的 XML 元素添加条件。例如,可以使用下面显示的代码在 Release模式中排除 DesignFriendsService。
  • 关闭 CSPROJ 文件。
  • 在解决方案资源管理器中右键单击卸载的项目,

  • 并选择重新加载项目。

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

    相关文章:

    wpf - Visual Studio 2012 XAML设计器无效标记

    silverlight - Xsl & Silverlight : Using XSLT and an algorithm, 如何打印出整个拉丁字母 (a,b,c..z)?

    c# - 如何在 Metro 风格应用程序中将值从一个 xaml 页面传递到另一个页面

    wpf - 在 WPF 中绑定(bind) DataGridTextColumn 可见性属性

    c# - 如何从 XAML 代码更改 ItemsPanelTemplate WrapGrid?

    WPF itemscontrol 绑定(bind)到控件集合

    c# - WPF On Start 检测进程是否启动并将应用程序置于最前面

    c# - C# 中的 Moq Application.Current.MainWindow

    c# - 事件不会跨模块触发(prism、MVVM、silverlight c#)

    Silverlight 路径模糊 - 如何对齐像素?