当前位置: 首页>>代码示例>>C#>>正文


C# ContentItem.GetOrCreate方法代码示例

本文整理汇总了C#中ContentItem.GetOrCreate方法的典型用法代码示例。如果您正苦于以下问题:C# ContentItem.GetOrCreate方法的具体用法?C# ContentItem.GetOrCreate怎么用?C# ContentItem.GetOrCreate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ContentItem的用法示例。


在下文中一共展示了ContentItem.GetOrCreate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: UpdateEditorAsync

        public async Task UpdateEditorAsync(ContentItem contentItem, UpdateEditorContext context)
        {
            var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(contentItem.ContentType);
            if (contentTypeDefinition == null)
                return;

            dynamic contentShape = context.Shape;
            dynamic partsShape = context.ShapeFactory.Create("Zone", Arguments.Empty);
            contentShape.Zones["Parts"] = partsShape;

            foreach (var displayDriver in _displayDrivers)
            {
                try
                {
                    var result = await displayDriver.UpdateEditorAsync(contentItem, context);
                    if (result != null)
                    {
                        result.Apply(context);
                    }
                }
                catch (Exception ex)
                {
                    InvokeExtensions.HandleException(ex, Logger, displayDriver.GetType().Name, nameof(UpdateEditorAsync));
                }
            }

            var partInfos = _contentPartDrivers.Select(x => x.GetPartInfo()).ToDictionary(x => x.PartName);

            foreach (var typePartDefinition in contentTypeDefinition.Parts)
            {
                ContentPartInfo partInfo;
                ContentPart part;

                if (partInfos.TryGetValue(typePartDefinition.PartDefinition.Name, out partInfo))
                {
                    // It's a well-known part type, bind the data to the model
                    part = partInfo.Factory(typePartDefinition);
                }
                else
                {
                    // Generic content part model (custom part)
                    part = new ContentPart();
                }

                part = contentItem.GetOrCreate(part.GetType(), () => new ContentPart(), typePartDefinition.Name) as ContentPart;

                // Create a custom shape to render all the part shapes into it
                dynamic typePartShape = context.ShapeFactory.Create("ContentPart_Edit", Arguments.Empty);
                typePartShape.ContentPart = part;
                typePartShape.ContentTypePartDefinition = typePartDefinition;

                var partPosition = typePartDefinition.Settings["Position"]?.ToString() ?? "before";

                partsShape.Add(typePartShape, partPosition);
                partsShape[typePartDefinition.Name] = typePartShape;

                context.FindPlacement = (shape, differentiator, displayType) => new PlacementInfo { Location = $"Parts.{typePartDefinition.Name}" };

                await _partDisplayDrivers.InvokeAsync(async contentDisplay =>
                {
                    var result = await contentDisplay.UpdateEditorAsync(part, typePartDefinition, context);
                    if (result != null)
                    {
                        result.Apply(context);
                    }
                }, Logger);

                foreach (var partFieldDefinition in typePartDefinition.PartDefinition.Fields)
                {
                    var fieldName = partFieldDefinition.Name;

                    var fieldPosition = partFieldDefinition.Settings["Position"]?.ToString() ?? "before";
                    context.FindPlacement = (shape, differentiator, displayType) => new PlacementInfo { Location = $"Parts.{typePartDefinition.Name}:{fieldPosition}" };

                    await _fieldDisplayDrivers.InvokeAsync(async contentDisplay =>
                    {
                        var result = await contentDisplay.UpdateEditorAsync(part, partFieldDefinition, typePartDefinition, context);
                        if (result != null)
                        {
                            result.Apply(context);
                        }
                    }, Logger);
                }
            }
        }
开发者ID:jchenga,项目名称:Orchard2,代码行数:85,代码来源:ContentItemDisplayCoordinator.cs


注:本文中的ContentItem.GetOrCreate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。