65.9K
CodeProject 正在变化。 阅读更多。
Home

仅在需要时嵌入

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.42/5 (5投票s)

2013年7月17日

CPOL

2分钟阅读

viewsIcon

12408

创建一个仅包含嵌入式资源的 DLL。

在继续尝试,并将 ASP.NET 控件和基于 jQuery 的 UI 的优点结合起来的过程中,我遇到了一个决定:创建一个仅包含嵌入式资源的 DLL。

这个 DLL 应该包含我所有的 JavaScript 文件作为嵌入式资源,并具有适当的机制,以便从整个应用程序的任何地方加载它们(包括依赖项和重复项)。

很明显,随着我将大量的 JavaScript 放入 DLL 中,在部署时这些文件必须被压缩。

很简单……我将“GoogleClosureCompiler”自定义工具添加到每个文件,然后 voilà!我完成了!

所以现在我在同一个项目中同时拥有代码的普通版本和压缩版本,所以我添加了一些条件来加载正确的版本……

类似这样的东西

using System;
using System.Web;
using System.Web.UI;

#if DEBUG
[assembly: WebResource( "jquery.js", "text/javascript" )]
[assembly: WebResource( "kendo.core.js", "text/javascript" )]
#else
[assembly: WebResource( "jquery.min.js", "text/javascript" )]
[assembly: WebResource( "kendo.core.min.js", "text/javascript" )]
#endif

namespace Utils
{
 public class JavaScript
 {
  private static int _IncludeID = 0;
  private static int _ScriptPath = 1;

  private static string _Script = 
    "<script javascript="javascript" src="\" text="text" type="\"></script>";

#if DEBUG
  private static string[ ] _jQuery = { "jQueryScriptInclude", "jquery.js" };
  private static string[ ] _Core = { "CoreScriptInclude", "kendo.core.js" };
#else
  private static string[ ] _jQuery = { "jQueryScriptInclude", "jquery.min.js" };
  private static string[ ] _Core = { "CoreScriptInclude", "kendo.core.min.js" };
#endif
  public static void Add ( Page Page, Type Type, string ID, string Name )
  {
   if ( !Page.ClientScript.IsStartupScriptRegistered( Type, ID ) )
   {
    Page.ClientScript.RegisterStartupScript( 
     Type, 
     ID, 
     string.Format( 
      _Script, 
      HttpUtility.HtmlEncode( Page.ClientScript.GetWebResourceUrl( Type, Name ) ) 
     ), 
     false 
    );
   }
  }
 }
}

看起来不错,不是吗?更好的是!它有效!加载了正确的 JavaScript 文件。

那么问题是什么?

普通版本和压缩版本的文件都嵌入到了 DLL 的调试版本和发布版本中!

事实上,当我开始思考这个问题时,发现这是预期的。没有人告诉编译器应该根据某些条件包含这些文件……经过一番搜索,我发现——显然——没有办法告诉编译器这样做!

有办法。虽然不美观,不舒适,没有 UI,但确实有办法。

你必须编辑项目文件。将文件移动到不同的 ItemGroup 中,并添加适当的条件。

最初,Visual Studio 创建的 ItemGroup 如下所示

<itemgroup>
 <embeddedresource include="js\jquery.js">
  <generator>GoogleClosureCompiler</generator>
  <lastgenoutput>jquery.min.js</lastgenoutput>
 </embeddedresource>
 <embeddedresource include="js\jquery.min.js">
  <autogen>True</autogen>
  <designtime>True</designtime>
  <dependentupon>jquery.js</dependentupon>
 </embeddedresource>
 <embeddedresource include="js\kendo.core.js">
  <generator>GoogleClosureCompiler</generator>
  <lastgenoutput>kendo.core.min.js</lastgenoutput>
 </embeddedresource>
 <embeddedresource include="js\kendo.core.min.js">
  <autogen>True</autogen>
  <designtime>True</designtime>
  <dependentupon>kendo.core.js</dependentupon>
 </embeddedresource>
</itemgroup>

你所要做的就是将普通版本和压缩版本的文件分成不同的 ItemGroup,然后添加一些条件,就完成了……

<itemgroup condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
 <embeddedresource include="js\jquery.js">
  <generator>GoogleClosureCompiler</generator>
  <lastgenoutput>jquery.min.js</lastgenoutput>
 </embeddedresource>
 <embeddedresource include="js\kendo.core.js">
  <generator>GoogleClosureCompiler</generator>
  <lastgenoutput>kendo.core.min.js</lastgenoutput>
 </embeddedresource>
</itemgroup>
<itemgroup condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
 <embeddedresource include="js\jquery.min.js">
  <autogen>True</autogen>
  <designtime>True</designtime>
  <dependentupon>jquery.js</dependentupon>
 </embeddedresource>
 <embeddedresource include="js\kendo.core.min.js">
  <autogen>True</autogen>
  <designtime>True</designtime>
  <dependentupon>kendo.core.js</dependentupon>
 </embeddedresource>
</itemgroup>

当然,手动执行这操作不太优雅,但也有好处。

Visual Studio 会保留你创建的 ItemGroup 定义,只要你不删除原始文件。而且考虑到我们拥有的扩展名数量,肯定有人已经在努力(已经完成?)添加一个处理这个小问题的工具……

© . All rights reserved.