0

我正在尝试将基于 Umbraco v7 构建的站点中的一些代码复制到基于 Umbraco v8 构建的站点中,但是新版本中似乎不存在某些类。这是我要复制的课程:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;

namespace XXXX.UmbracoStarterKit.CMS.Models
{
    // Mixin content Type 1245 with alias "baseImage"
    /// <summary>Base Image</summary>
    public partial interface IBaseImage : IPublishedContent
    {
        /// <summary>Alternative Text</summary>
        string ImageAlt { get; }

        /// <summary>Bytes</summary>
        string UmbracoBytes { get; }

        /// <summary>Extension</summary>
        string UmbracoExtension { get; }

        /// <summary>Height</summary>
        string UmbracoHeight { get; }

        /// <summary>Width</summary>
        string UmbracoWidth { get; }
    }

    /// <summary>Base Image</summary>
    [PublishedContentModel("baseImage")]
    public partial class BaseImage : PublishedContentModel, IBaseImage
    {
#pragma warning disable 0109 // new is redundant
        public new const string ModelTypeAlias = "baseImage";
        public new const PublishedItemType ModelItemType = PublishedItemType.Media;
#pragma warning restore 0109

        public BaseImage(IPublishedContent content)
            : base(content)
        { }

#pragma warning disable 0109 // new is redundant
        public new static PublishedContentType GetModelContentType()
        {
            return PublishedContentType.Get(ModelItemType, ModelTypeAlias);
        }
#pragma warning restore 0109

        public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<BaseImage, TValue>> selector)
        {
            return PublishedContentModelUtility.GetModelPropertyType(GetModelContentType(), selector);
        }

        ///<summary>
        /// Alternative Text: The text used if  the image cannot be displayed. This can be useful for SEO and accessibility.
        ///</summary>
        [ImplementPropertyType("imageAlt")]
        public string ImageAlt
        {
            get { return GetImageAlt(this); }
        }

        /// <summary>Static getter for Alternative Text</summary>
        public static string GetImageAlt(IBaseImage that) { return that.GetPropertyValue<string>("imageAlt"); }

        ///<summary>
        /// Bytes: The size of the image in bytes.
        ///</summary>
        [ImplementPropertyType("umbracoBytes")]
        public string UmbracoBytes
        {
            get { return GetUmbracoBytes(this); }
        }

        /// <summary>Static getter for Bytes</summary>
        public static string GetUmbracoBytes(IBaseImage that) { return that.GetPropertyValue<string>("umbracoBytes"); }

        ///<summary>
        /// Extension: The image extension.
        ///</summary>
        [ImplementPropertyType("umbracoExtension")]
        public string UmbracoExtension
        {
            get { return GetUmbracoExtension(this); }
        }

        /// <summary>Static getter for Extension</summary>
        public static string GetUmbracoExtension(IBaseImage that) { return that.GetPropertyValue<string>("umbracoExtension"); }

        ///<summary>
        /// Height: The height of the image.
        ///</summary>
        [ImplementPropertyType("umbracoHeight")]
        public string UmbracoHeight
        {
            get { return GetUmbracoHeight(this); }
        }

        /// <summary>Static getter for Height</summary>
        public static string GetUmbracoHeight(IBaseImage that) { return that.GetPropertyValue<string>("umbracoHeight"); }

        ///<summary>
        /// Width: The width of the image.
        ///</summary>
        [ImplementPropertyType("umbracoWidth")]
        public string UmbracoWidth
        {
            get { return GetUmbracoWidth(this); }
        }

        /// <summary>Static getter for Width</summary>
        public static string GetUmbracoWidth(IBaseImage that) { return that.GetPropertyValue<string>("umbracoWidth"); }
    }
}

上面public partial class BaseImage有一个属性 ( PublishedContentModel("baseImage")),但这在 v8 中似乎不存在。在 v7 中,当我查看定义时,我看到了 PublishedContentModelAttribute,它是 Umbraco.Core.Models.PublishedContent 命名空间的一部分。在v8中有替代品吗?

在 GetModelContentType 方法中有对 PublishedContentType.Get 的调用,但在 v8 中的 Umbraco.Core.Models.PublishedContent 命名空间中也缺少 Get 方法。有什么建议我可以改用吗?

在 GetModelPropertyType 方法中有对 PublishedContentModelUtility.GetModelPropertyType 的调用,但 PublishedContentModelUtility 似乎不再存在于 Umbraco.ModelBuilder.Umbraco 中。v8有替代品吗?

此外,还有几个对 that.GetPropertyValue 的调用,它们应该在 Umbraco.Web.PublishedContentExtenions 命名空间中,但在 v8 中找不到。谁能建议我可以使用什么代码?

我意识到这里有几个问题,但它们都是同一个问题的一部分,都阻止我完成任务。

4

1 回答 1

0

看看https://our.umbraco.com/forum/umbraco-8/98400-errors-throw-after-upgrade-to-811

从 v7 到 v8 发生了很多变化,包括一些 ModelsBuilder 的东西。看起来其中一些现在正在使用接口。也许让 Umbraco 8 ModelsBuilder 为您创建一个全新的模型类,然后看看您是否可以通过这种方式发现变化?

于 2019-10-28T13:57:19.153 回答