1

我正在使用 Orchard CMS 和 Bootstrap 主题。我一直在尝试使用引导程序中内置的轮播来工作,如以下博客文章所示:http: //www.stevetaylor.me.uk/image-carousel-using-twitter-bootstrap-and-orchard-cms-projections . 我已按照教程进行操作,但我无法让我的新布局文件出现在网格和 html 列表下的查询中。我相信我已经逐字复制了代码,但仍然可以让它工作。任何人都请帮忙,因为认为这将是一个很棒的功能。如果我让它工作,我会要求它被添加到这里的引导主题:http: //orchardbootstrap.codeplex.com/

请参见下面的代码:

CarouselLayoutForm.cs

using System;
using Orchard.DisplayManagement;
using Orchard.Forms.Services;
using Orchard.Localization;

namespace Orchard.Projections.Providers.Layouts {

public class CarouselLayoutForms : IFormProvider {
    protected dynamic Shape { get; set; }
    public Localizer T { get; set; }

    public CarouselLayoutForms(
        IShapeFactory shapeFactory) {
        Shape = shapeFactory;
        T = NullLocalizer.Instance;
    }

    public void Describe(DescribeContext context) {
        Func<IShapeFactory, object> form =
            shape => {

                var f = Shape.Form(
                    Id: "CarouselLayout",                        
                    _HtmlProperties: Shape.Fieldset(
                        Title: T("Html properties"), 
                        _ListId: Shape.TextBox(
                            Id: "outer-grid-id", Name: "OuterDivId",
                            Title: T("Outer div id"),
                            Description: T("The id to provide on the div element."),
                            Classes: new[] { "textMedium", "tokenized" }
                            ),
                        _ListClass: Shape.TextBox(
                            Id: "outer-div-class", Name: "OuterDivClass",
                            Title: T("Outer div class"),
                            Description: T("The class to provide on the div element."),
                            Classes: new[] { "textMedium", "tokenized" }
                            ),
                        _InnerClass: Shape.TextBox(
                            Id: "inner-div-class", Name: "InnerDivClass",
                            Title: T("Inner div class"),
                            Description: T("The class to provide on the inner div element."),
                            Classes: new[] { "textMedium", "tokenized" }
                            ),
                         _FirstItemClass: Shape.TextBox(
                            Id: "first-item-class", Name: "FirstItemClass",
                            Title: T("First item class"),
                            Description: T("The class to provide on the first item element."),
                            Classes: new[] { "textMedium", "tokenized" }
                            ),
                        _ItemClass: Shape.TextBox(
                            Id: "item-class", Name: "ItemClass",
                            Title: T("Item class"),
                            Description: T("The class to provide on the item element."),
                            Classes: new[] { "textMedium", "tokenized" }
                            )
                        )
                    );

                return f;
            };

        context.Form("CarouselLayout", form);

    }
}
   /*
public class CarouselLayoutFormsValitator : FormHandler {
    public Localizer T { get; set; }

    public override void Validating(ValidatingContext context) {
        if (context.FormName == "CarouselLayout") {
            if (context.ValueProvider.GetValue("Alignment") == null) {
                context.ModelState.AddModelError("Alignment", T("The field Alignment is required.").Text);
            }

            if (context.ValueProvider.GetValue("Columns") == null) {
                context.ModelState.AddModelError("Columns", T("The field Columns/Lines is required.").Text);
            }
            else {
                int value;
                if (!Int32.TryParse(context.ValueProvider.GetValue("Columns").AttemptedValue, out value)) {
                    context.ModelState.AddModelError("Columns", T("The field Columns/Lines must be a valid number.").Text);
                }
            }
        }
    }
}
*/

}

轮播布局.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Projections.Descriptors.Layout;
using Orchard.Projections.Models;
using Orchard.Projections.Services;

namespace Orchard.Projections.Providers.Layouts {
public class CarouselLayout : ILayoutProvider {
    private readonly IContentManager _contentManager;
    protected dynamic Shape { get; set; }

    public CarouselLayout(IShapeFactory shapeFactory, IContentManager contentManager) {
        _contentManager = contentManager;
        Shape = shapeFactory;
        T = NullLocalizer.Instance;
    }

    public Localizer T { get; set; }

    public void Describe(DescribeLayoutContext describe) {
        describe.For("Html", T("Html"),T("Html Layouts"))
            .Element("Carousel", T("Carousel"), T("Organizes content items in a carousel."),
                DisplayLayout,
                RenderLayout,
                "CarouselLayout"
            );
    }

    public dynamic RenderLayout(LayoutContext context, IEnumerable<LayoutComponentResult> layoutComponentResults) {

        string outerDivClass = context.state.outerDivClass;
        string OuterDivId = context.state.OuterDivID;
        string innerDivClass = context.state.InnerDicClass;
        string firstItemClass = context.state.FirstItemClass;
        string itemClass = context.state.ItemClass;

        IEnumerable<dynamic> shapes =
           context.LayoutRecord.Display == (int)LayoutRecord.Displays.Content
               ? layoutComponentResults.Select(x => _contentManager.BuildDisplay(x.ContentItem, context.LayoutRecord.DisplayType))
               : layoutComponentResults.Select(x => x.Properties);

        return Shape.Carousel(Id: outerDivId, Items: shapes, OuterClasses: new[] { outerDivClass }, 
                InnerClasses: new[] {innerDivClass}, FirstItemClasses: new[] {firstItemClass}, ItemClasses: new[] {itemClass});
    }
}
}

布局形状.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc.Html;
using Orchard.Utility.Extensions;

namespace Orchard.Projections.Providers.Layouts {
public class LayoutShapes : IDependency {
    public LayoutShapes() {
        T = NullLocalizer.Instance;
    }

    public Localizer T { get; set; }

    [Shape]
    public void Carousel(dynamic Display, TextWriter Output, HtmlHelper Html, string Id, IEnumerable<dynamic> Items, 
                        IEnumerable<string> OuterClasses, IDictionary<string, string> OuterAttributes,
                        IEnumerable<string> InnerClasses, IDictionary<string, string> InnerAttributes, 
                        IEnumerable<string> FirstItemClasses, IDictionary<string, string> FirstItemAttributes, IEnumerable<string> ItemClasses,  IDictionary<string, string> ItemAttributes ) 
    {            
        if (Items == null) return;

        var items = Items.ToList();
        var itemsCount = items.Count;

        if (itemsCount < 1) return;

        var outerDivTag = GetTagBuilder("div", Id, OuterClasses, OuterAttributes);
        var innerDivTag = GetTagBuilder("div", string.Empty, InnerClasses, InnerAttributes);
        var firstItemTag = GetTagBuilder("div", string.Empty, FirstItemClasses, FirstItemAttributes);
        var itemTag = GetTagBuilder("div", string.Empty, ItemClasses, ItemAttributes);

        Output.Write(outerDivTag.ToString(TagRenderMode.StartTag));
        Output.Write(innerDivTag.ToString(TagRenderMode.StartTag));

        int i = 0;

        foreach (var item in items)
        {
            if (i== 0)
               Output.Write(firstItemTag.ToString(TagRenderMode.StartTag)); 
            else
               Output.Write(itemTag.ToString(TagRenderMode.StartTag)); 

            Output.Write(Display(item));
            Output.Write(itemTag.ToString(TagRenderMode.EndTag));
            i++;
        }

        Output.Write(innerDivTag.ToString(TagRenderMode.EndTag));

        Output.Write("<a href=\"#{0}\" class=\"carousel-control left\" data-slide=\"prev\">&lsaquo;</a>",id);
        Output.Write("<a href=\"#{0}\" class=\"carousel-control right\" data-slide=\"next\">&lsaquo;</a>",id);

        Output.Write(outerDivTag.ToString(TagRenderMode.EndTag));

        Output.Write("<script>$(function () {$('"+ Id +"').carousel();}); </script>");

    }

    static TagBuilder GetTagBuilder(string tagName, string id, IEnumerable<string> classes, IDictionary<string, string> attributes) {
        var tagBuilder = new TagBuilder(tagName);
        tagBuilder.MergeAttributes(attributes, false);
        foreach (var cssClass in classes ?? Enumerable.Empty<string>())
            tagBuilder.AddCssClass(cssClass);
        if (!string.IsNullOrWhiteSpace(id))
            tagBuilder.GenerateId(id);
        return tagBuilder;
    }

}
}
4

2 回答 2

0

您是否按照说明将代码添加到模块 Orchard.Projections 中?您需要在 Visual Studio 中添加文件并保存项目以将它们包含在 .csproj 中(除非动态编译不会考虑这些文件),或者您可以构建模块。

于 2012-11-02T20:55:50.390 回答
0

您在 CarouselLayou.cs 文件中缺少 DisplayLayout() 的定义:

    public LocalizedString DisplayLayout(LayoutContext context)
    {
        string columns = context.State.Columns;
        bool horizontal = Convert.ToString(context.State.Alignment) != "vertical";

        return horizontal
                   ? T("{0} columns grid", columns)
                   : T("{0} lines grid", columns);
    }

此外,LayourShapes.cs 中的变量“id”应该是“Id”,并且声明中还有其他不正确的大小写:

string outerDivClass = context.state.outerDivClass;
string OuterDivId = context.state.OuterDivID;
string innerDivClass = context.state.InnerDicClass;
string firstItemClass = context.state.FirstItemClass;
string itemClass = context.state.ItemClass;

应该:

string outerDivClass = context.State.OuterDivClass;
string outerDivId = context.State.OuterDivID;
string innerDivClass = context.State.InnerDicClass;
string firstItemClass = context.State.FirstItemClass;
string itemClass = context.State.ItemClass;
于 2014-05-21T13:03:20.243 回答