简体   繁体   English

使用ClientDependency Framework动态添加CSS或JS的正确方法是什么?

[英]What is the proper way to dynamically add CSS or JS using ClientDependency Framework?

I'm sorry to say, but the documentation is somewhat lacking here: https://github.com/Shazwazza/ClientDependency/wiki/Webforms 我很遗憾地说,但文档有点缺乏: https//github.com/Shazwazza/ClientDependency/wiki/Webforms

I'm trying to register a dependency dynamically and cannot get the syntax right: 我正在尝试动态注册依赖项,但无法正确获取语法:

<%@ Page Language="C#" AutoEventWireup="false" %>
<%@ Register TagPrefix="CD" Namespace="ClientDependency.Core.Controls" Assembly="ClientDependency.Core" %>

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        if (Request.Cookies["AAPGmobile"] != null && Request.Cookies["AAPGmobile"].Value == "Mobile")
        {
            //mobile
            ClientDependencyLoader.RegisterDependency("~/Style/mobile.css",ClientDependencyType.Css);
        }
        else
        {
            //desktop
            ClientDependencyLoader.RegisterDependency("~/Style/desktop.css",ClientDependencyType.Css);
        }
    }
</script>

Document HEAD: 文件HEAD:

<CD:ClientDependencyLoader runat="server" id="Loader" />
<asp:PlaceHolder runat="server" ID="CssPlaceHolder"></asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="JavaScriptPlaceHolder"></asp:PlaceHolder>

I also tried using this, but got an error: 我也尝试过使用它,但是出现了错误:

bool successfullyCreated;
var loader = ClientDependencyLoader.TryCreate(this.Page, out successfullyCreated);

And this, but got a different error: 而这,但有一个不同的错误:

var loader = ClientDependencyLoader.GetInstance(currentHttpContext);

Please help with the syntax or point me in the right direction. 请帮助语法或指出我正确的方向。 Thanks! 谢谢!

This is the solution I found that seems to work – I'm posting it here in case anyone else runs into a similar situation: 这是我发现似乎有效的解决方案 - 我在这里发布它以防其他人遇到类似的情况:

For CSS: 对于CSS:

ClientDependencyLoader.Instance.RegisterDependency("~/Style/mobile.css",ClientDependency.Core.ClientDependencyType.Css);

For JS: 对于JS:

ClientDependencyLoader.Instance.RegisterDependency("~/Style/mobile.js",ClientDependency.Core.ClientDependencyType.Javascript);

Please notice, I'm not trying to create or reference an existing Loader separately, the syntax takes care of it. 请注意,我不是要单独创建或引用现有的Loader,语法会处理它。

The solution is adapted from code on this page: https://our.umbraco.org/forum/developers/razor/35307-Using-ClientDependency-Framework-from-a-Razor-Macro 该解决方案改编自此页面上的代码: https//our.umbraco.org/forum/developers/razor/35307-Using-ClientDependency-Framework-from-a-Razor-Macro

Hope this helps somebody! 希望这有助于某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM