SharePoint:使用 CssRegistration 类注册 CSS 文件时排序 CSS 引用时出现问题

发布于 2024-10-04 00:11:08 字数 2493 浏览 0 评论 0原文

我正在开发一个需要一些自定义 CSS 文件的 Web 部件。因此,我使用 CssRegistration 类将它们添加到页眉中。

该代码注册了 4 个 CSS 文件,这些文件通过 Web 部件功能部署到布局文件夹中。当 Web 部件的属性 AdditionalCss 中设置了第五个 CSS 文件的路径时,可以选择注册第五个 CSS 文件。 CSS 文件应插入到标头中的所有 SharePoint CSS 文件之后,并应按代码添加的顺序排序。

我使用的代码如下:

var contentCss = new CssRegistration 
                 { Name = "/_layouts/MyWebPart/css/content.css", 
                   RevealToNonIE = true };

if (SPContext.Current.Web.UIVersion == 4)
     contentCss.After = "corev4.css";
else
     contentCss.After = "core.css";

Controls.Add(contentCss);

var customCss = new CssRegistration 
                { Name = "/_layouts/MyWebPart/css/cn_custom.css",
                  After = contentCss.Name, RevealToNonIE = true };
Controls.Add(customCss);

var styleCss = new CssRegistration 
               { Name = "/_layouts/MyWebPart/css/styles.css", 
                 After = customCss.Name, RevealToNonIE = true };
Controls.Add(styleCss);

var colorsCss = new CssRegistration 
                { Name = "/_layouts/MyWebPart/css/colors.css",
                  After = styleCss.Name, RevealToNonIE = true};
Controls.Add(colorsCss);            

if (!string.IsNullOrEmpty(AdditionalCss))
{
    var webPartCustomCss = new CssRegistration 
                           { Name = AdditionalCss, 
                             After = colorsCss.Name, 
                             RevealToNonIE = true };            
     Controls.Add(webPartCustomCss);
}

当我将 Web 部件添加到页面时,所有 CSS 文件都会按预期添加到页面。除了文件按错误的顺序排序之外。

如果没有自定义 CSS 文件,顺序是:(链接的 rel- 和 type-attribute 已被删除,以便更好地概览)

...
<link href="/_layouts/1033/styles/Themable/corev4.css"/>
<link href="/_layouts/MyWebPart/css/colors.css"/>
<link href="/_layouts/MyWebPart/css/content.css"/>
<link href="/_layouts/MyWebPart/css/cn_custom.css"/>
<link href="/_layouts/MyWebPart/css/styles.css"/>

使用自定义 CSS 文件,顺序是:

... 
<link href="/_layouts/1033/styles/Themable/corev4.css"/>
<link href="/_layouts/MyWebPart/css/cn_custom.css"/>
<link href="/sites/mysite/Style%2520Library/de-de/test.css"/>
<link href="/_layouts/MyWebPart/css/styles.css"/>
<link href="/_layouts/MyWebPart/css/content.css"/>
<link href="/_layouts/MyWebPart/css/colors.css"/>

正如您所看到的,两种情况都提供了完全不同的顺序,并且 CSS 文件从来没有按代码添加的顺序排序。

由于这种奇怪的行为,整个 CssRegistration 类不是很有用,因为你不能 传递 CSS 文件始终保持相同的顺序。这使得使用 CSS 进行设计几乎不可能。

I'm developing a web part which needs some custom CSS files. So I'm using the CssRegistration class to add them to the page header.

The code registers 4 CSS files which got deployed to the layouts folder by the web part feature. A fifth CSS files is optionally registered when there's a path to it set in the web part's property AdditionalCss. The CSS files should be inserted in the header after all SharePoint CSS files and should be sorted in the order they were added by code.

The code I used is the following:

var contentCss = new CssRegistration 
                 { Name = "/_layouts/MyWebPart/css/content.css", 
                   RevealToNonIE = true };

if (SPContext.Current.Web.UIVersion == 4)
     contentCss.After = "corev4.css";
else
     contentCss.After = "core.css";

Controls.Add(contentCss);

var customCss = new CssRegistration 
                { Name = "/_layouts/MyWebPart/css/cn_custom.css",
                  After = contentCss.Name, RevealToNonIE = true };
Controls.Add(customCss);

var styleCss = new CssRegistration 
               { Name = "/_layouts/MyWebPart/css/styles.css", 
                 After = customCss.Name, RevealToNonIE = true };
Controls.Add(styleCss);

var colorsCss = new CssRegistration 
                { Name = "/_layouts/MyWebPart/css/colors.css",
                  After = styleCss.Name, RevealToNonIE = true};
Controls.Add(colorsCss);            

if (!string.IsNullOrEmpty(AdditionalCss))
{
    var webPartCustomCss = new CssRegistration 
                           { Name = AdditionalCss, 
                             After = colorsCss.Name, 
                             RevealToNonIE = true };            
     Controls.Add(webPartCustomCss);
}

When I add the web part to a page all CSS files are added to the page as expected. Except the files are sorted in the wrong order.

Without the custom CSS file the order is: (link's rel- and type-attribute were removed for a better overview)

...
<link href="/_layouts/1033/styles/Themable/corev4.css"/>
<link href="/_layouts/MyWebPart/css/colors.css"/>
<link href="/_layouts/MyWebPart/css/content.css"/>
<link href="/_layouts/MyWebPart/css/cn_custom.css"/>
<link href="/_layouts/MyWebPart/css/styles.css"/>

With the custom CSS file the order is:

... 
<link href="/_layouts/1033/styles/Themable/corev4.css"/>
<link href="/_layouts/MyWebPart/css/cn_custom.css"/>
<link href="/sites/mysite/Style%2520Library/de-de/test.css"/>
<link href="/_layouts/MyWebPart/css/styles.css"/>
<link href="/_layouts/MyWebPart/css/content.css"/>
<link href="/_layouts/MyWebPart/css/colors.css"/>

As you can see both cases provide a totally different order and the CSS files were never sorted in the order they were added by code.

With this strange behavior the whole CssRegistration class is not very useful as you cannot
relay that the CSS files are always in the same order. This makes designing with CSS nearly impossible.

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

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

发布评论

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

评论(1

情魔剑神 2024-10-11 00:11:08

您看过 CssRegistration 控件的“After”属性吗?我认为,如果您不使用此属性指定顺序,那么结果将按源中的字母顺序排列。这与您所看到的并不完全相符,但可能还有其他原因导致您的行为不一致。

http://www .wictorwilen.se/Post/What-is-new-with-the-CssRegistration-control-in-SharePoint-2010.aspx

Have you looked at the "After" property of the CssRegistration control? I think that if you don't specify an order using this property then the results are in alphabetical order in your source. This doesn't exactly match what you see but there may be something else going on that is causing your inconsistent behavior.

http://www.wictorwilen.se/Post/What-is-new-with-the-CssRegistration-control-in-SharePoint-2010.aspx

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