如何处理类库中具有文件依赖关系的 Web 控件

发布于 2024-12-02 05:45:00 字数 1369 浏览 2 评论 0原文

如果可能的话,我可以提供一些指导。

我正在构建一个类库来包含自定义 Web 控件。我已将许多 jQuery UI 元素转换为 .NET 类,以便在页面中动态使用。例如,

    Dim Msg As New Dialog("Dialog Title", New LiteralControl("Dialog Content"))
    Msg.Width.Value = 500
    Msg.Height.Value = 300
    Me.Controls.Add(Msg )

必要的脚本在CreateChildControls 期间被插入到头部,并且任何jQuery 文件引用都被添加到头部。例如,

    RegisterScriptFile("~/Scripts/jquery-1.5.1.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.core.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.widget.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.mouse.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.draggable.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.position.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.resizable.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.dialog.js")

WebControl 基类负责将引用插入到页面头部。

这很棒,但是有一个问题......使用控制库的应用程序之间的文件路径可能不同。

我可以看到的选择是:

A)将文件作为资源嵌入到库本身中......但是,CSS 样式在各个应用程序之间是不可自定义的,并且对 CSS/JS 的任何更改都需要重新编译。

B) 定义并使用标准化文件层次结构。每个需要文件夹的应用程序都调用“脚本”,其中包含固定的文件层次结构,因此控件知道在哪里引用所需的文件。我在这里看到的问题是,可能并不总是可以使用这种标准化的层次结构,并且可能会使使用库变得很麻烦。

C) 为每个控件及其所需的每个文件创建一个属性。同样,这会变得很麻烦并且使用起来很痛苦,因为每个实例化的控件都必须设置这些属性。

D) 创建某种类似 ResourceUrlLibrary 字典的类,消费应用程序可以填充该类,然后在实例化时提供给每个控件。然而,这似乎很复杂,可能会给其他开发人员带来困惑。

如果有人遇到过这个问题并且能给我一些指导,那就太好了:)

I could do with a little guidance if possible..

I'm building a class library to contain custom web controls. I've transformed many of the jQuery UI elements into .NET classes for dynamic use in pages. e.g.

    Dim Msg As New Dialog("Dialog Title", New LiteralControl("Dialog Content"))
    Msg.Width.Value = 500
    Msg.Height.Value = 300
    Me.Controls.Add(Msg )

The necessary scripts get inserted into the head during CreateChildControls, and any jQuery file references are added to the head e.g.

    RegisterScriptFile("~/Scripts/jquery-1.5.1.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.core.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.widget.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.mouse.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.draggable.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.position.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.resizable.js")
    RegisterScriptFile("~/Scripts/UI/jquery.ui.dialog.js")

The WebControl base class handles inserting the references into the Page Head.

This is brilliant, however there's a problem...the file paths may differ between applications which consume the control library.

My choices that I can see are:

A) Embed the files as resources within the library itself....however the CSS styling would be non-customisable between the individual aplications, and any changes to CSS/JS would need a re-compile.

B) Define and use a standardised file heirarchy. Each application needing a folder call 'Scripts' with a fixed file heirarchy within, so the control knows where to reference the required files. The problem I can see here is that it might not always be possible to use this standardised heirarchy and could make using the library cumbersome.

C) Create a property for each control, for each file it requires. Again this would become cumbersome and a pain to use, because each instantiated control would have to have those properties set.

D) Make some kind of ResourceUrlLibrary Dictionary like class which the consuming app can populate, then give to each control as it's instantiated. However, this seems convoluted and could cause confusion for other developers.

If anyone has come across this problem and could spare me some guidance that would be brilliant :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-12-09 05:45:00

如果它是自定义控件,则它具有对 Page 类的引用,然后该类具有调用 ResolveClientUrl 的方法来为您生成相对 URL。这样就可以解决这种情况。您可能希望公开 ScriptsFolder 属性,该属性允许您存储脚本的路径,而不是对其进行硬编码。

如果这是针对您自己的自定义项目,则标准化文件夹就可以了,但如果您要创建要重用的公共库,则需要特定文件夹并不是一个好主意,然后您可以使用 ScriptsFolder 属性来解决此问题,或者将文件夹路径存储在 config.xml 中。标准化应用程序设置的使用是可以的。

为了确认这一点,我使用 Telerik 控件,它们采用了一个属性来定义脚本的自定义路径(因为它们依赖于特定控件),并且它们还具有某些设置,可以通过添加应用程序设置。

HTH。

If its a custom control, it has a reference to the Page class, which then has a method call ResolveClientUrl to generate a relative URL for you. So that can take care of that scenario. You may want to expose a ScriptsFolder property that allows you to store the path to the scripts rather than hard-coding it too.

If this is for your own custom project, standardizing on a folder is fine, but if you are creating a common library to be reused, requiring a specific folder isn't a good idea, and you can then use the ScriptsFolder property to remedy this, or store the folder path in the config. It's OK to standardize on the use of an application setting.

To confirm, I use Telerik controls, and they go the route of having a property that defines a custom path to the script (since they rely on one for a specific control), and they also have certain settings that can be overridden by adding an application setting.

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文