为什么ContentPart不在ContentItem中?

时间:2012-12-07 12:30:54

标签: orchardcms

为什么无场的ContentPart不包含在ContentItem中?

以下是Migrations.cs的代码:

SchemaBuilder.CreateTable("ImageDescribedPartRecord", 
                table => table.ContentPartRecord());

ContentDefinitionManager.AlterPartDefinition(
                "ImageDescribedPart",
                cpd => cpd.WithField(
                    "Image",
                    b => b
                             .OfType("MediaPickerField")
                             .WithSetting("MediaPickerFieldSettings.Required", "false")));

ContentDefinitionManager.AlterTypeDefinition(
                "PlantPicture",
                cfg => cfg
                           .WithPart("ImageDescribedPart")
                           .WithPart("CommonPart", p => p.WithSetting("OwnerEditorSettings.ShowOwnerEditor", "false"))
                           .WithPart("BodyPart")
                           .WithPart("TitlePart")
                           .WithPart("AutoroutePart"
                                     , builder =>
                                       builder
                                           .WithSetting("AutorouteSettings.AllowCustomPattern", "false")
                                           .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "true")
                                           .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Container', Pattern: '{Content.Container.Path}/images/{Content.Slug}', Description: 'apgii/taxon/sub-taxon/images/title'}]")
                                           .WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
                           .WithPart("ContainablePart")
                           .Creatable(true)
                    // todo: The following doesn't work. Make it work.
                           .WithSetting("BodyPartSettings.FlavorDefault", "text") 
                           );

以下是ContentPart的代码:

public class ImageDescribedPart : ContentPart<ImageDescribedPartRecord>{

    }

 public class ImageDescribedPartRecord :ContentPartRecord {}

来自驱动程序的以下代码

IContentQuery<ContentItem> query = _contentManager
     .Query(VersionOptions.Published)
    .Join<CommonPartRecord>()
    .Where(cr => cr.Container.Id == container.Id);
var items = query.Slice(0, 10).ToList();

IEnumerable<Zulatm.WebPlants.Models.ImageDescribedPart> firstImages = items.AsPart<ImageDescribedPart>();

Logger.Debug("Items count: {0}", items.Count());
for (int i = 0; i < items.Count(); i++) {
    Logger.Debug("Item {0}: {1}", i, items[i].As<TitlePart>().Title);

}
Logger.Debug("Images count: {0}",firstImages.Count());

返回以下内容

2012-12-07 16:28:45,616 [35] TaxonomyNodePartDriver - Items count: 2
2012-12-07 16:28:45,617 [35] TaxonomyNodePartDriver - Item 0: Test
2012-12-07 16:28:45,619 [35] TaxonomyNodePartDriver - Item 1: test img 2
2012-12-07 16:28:45,619 [35] TaxonomyNodePartDriver - Images count: 0

2 个答案:

答案 0 :(得分:1)

您将项目“投射”到零件并将其存储为IEnumerable。我想知道这不会导致编译错误,因为这是错误的。

你问题的根源很可能是如果你计划直接通过“强制转换”使用零件,那么零件应该有一个相应的驱动程序(可以是空的)。

答案 1 :(得分:1)

确定。问题是Handler中没有添加过滤器。添加了ActivatorFilter:

public ImageDescribedHandler (IRepository<ImageDescribedPartRecord> repository,      INavigationManager navigationManager)
{
    Filters.Add(new ActivatingFilter<ImageDescribedPart>("PlantPicture"));
}

同时删除了创建表格的行。