学习011-03-06 Change the Format Used for the FullAddress and FullName Properties

Change the Format Used for the FullAddress and FullName Properties(更改 FullAddress 和 FullName 属性使用的格式)

There are FullAddress and FullName properties in the Address and Person business classes that are supplied with the Business Class Library. These properties are calculable. The FullAddress property represents a string formed by the concatenation of the Country.Name, StateProvince, City, Street and ZipPostal property values. The FullName property is formed by the concatenation of the FirstName, MiddleName and LastName property values. The FullAddress and FullName properties are implemented so that you can change the order in which the items are concatenated. This topic details how to change this order. You can use this technique when implementing analogous business class properties.
业务类库提供的Address和Person业务类中有FullAddress和FullName属性。这些属性是可计算的。FullAddress属性表示由Country.Name、State省、城市、街道和ZipPostal属性值串联而成的字符串。FullName属性由FirstName、MiddleName和LastName属性值串联而成。实现FullAddress和FullName属性是为了更改项目串联的顺序。本主题详细介绍了如何更改此顺序。您可以在实现类似的业务类属性时使用此技术。

The following images demonstrate how the FullAddress and FullName properties are calculated.
下图演示了如何计算FullAddress和FullName属性。
在这里插入图片描述

在这里插入图片描述

To format FullAddress and FullName properties, the ObjectFormatter.Format method of the helper ObjectFormatter class is used. In this method, the format, according to which the property value is generated, is taken as a parameter. The format passed for the FullAddress property is specified by the Address class’ FullAddressFormat property. The format that is passed for the FullName property is specified by the Person class’ FullNameFormat property. Note that FullAddress and FullName properties are non-persistent calculated properties. As such, they require persistent aliases to be created for them to support sorting in Server, ServerView, InstantFeedback, and InstantFeedbackView mode (see CollectionSourceBase.DataAccessMode). For this purpose, there are additional FullNamePersistentAlias and FullAddressPersistentAlias properties. These properties are used to create persistent aliases.
要格式化FullAddress和FullName属性,需要使用辅助程序ObjectFormat类的ObjectFormat. Format方法。在此方法中,将生成属性值所依据的格式作为参数。为FullAddress属性传递的格式由Address类的FullAddressFormat属性指定。为FullName属性传递的格式由Person类的FullNameFormat属性指定。请注意,FullAddress和FullName属性是非持久计算属性。因此,它们需要为它们创建持久别名以支持Server、ServerView、InstantFeedback和InstantFeedbackView模式下的排序(参见CollectionSourceBaseDataAccessMode)。为此,有额外的FullNamePersistentAlias和FullAddressPersistentAlias属性。这些属性用于创建持久别名。

By default, the FullAddressFormat property is set to the Address class’ defaultFullAddressFormat constant, which is the following: “{Country.Name}; {StateProvince}; {City}; {Street}; {ZipPostal}”. The FullAddressFormatPersistentAlias property is set to the Address class’ defaultFullAddressPersistentAlias constant, which is the following: “concat(Country.Name, StateProvince, City, Street, ZipPostal)”. To change these property values, use the SetFullAddressFormat method (SetFullNameFormat for the Person class). These methods are static, so you can call them any place in your solution. For instance, you can specify the required format in the configuration file and read its value in a module’s constructor. In addition, override the module’s ModuleBase.CustomizeTypesInfo method and process the CalculatedPersistentAlias attribute via the static CalculatedPersistentAliasHelper.CustomizeTypesInfo method.
默认情况下,FullAddressFormat属性设置为Address类的defaultFullAddressFormat常量,如下所示:“{Country.Name};{State省};{城市};{街道};{ZipPostal}”。FullAddressFormatPersistentAlias属性设置为Address类的defaultFullAddressPersistentAlias常量,如下所示:“conat(Country.Name, State省,城市,街道,ZipPostal)”。要更改这些属性值,请使用SetFullAddressFormat方法(SetFullNameFormat for the Person类)。这些方法是静态的,因此您可以在解决方案中的任何位置调用它们。例如,您可以在配置文件中指定所需的格式,并在模块的构造函数中读取其值。此外,覆盖模块的ModuleBase.CustomizeTypesInfo方法并通过静态CalculatedPersistentAliasHelperCustomizeTypesInfo方法处理CalculatedPersistentAlias属性。

XML 
<configuration>
    <appSettings>
        <add key="FullAddressFormat" value="{Country.Name} {City} {Street}" />
        <add key="FullAddressFormatPersistentAlias" value="concat(Country.Name, City, Street)" />
        <!-- ... -->
    </appSettings>
</configuration>
C# 
using DevExpress.Persistent.BaseImpl;
using System.Configuration;
//...
public sealed partial class MainDemoModule : ModuleBase {
    static MainDemoModule() {
        Address.SetFullAddressFormat(ConfigurationManager.AppSettings["FullAddressFormat"], 
            ConfigurationManager.AppSettings["FullAddressFormatPersistentAlias"]);
    }
    public override void CustomizeTypesInfo(ITypesInfo typesInfo) {
        base.CustomizeTypesInfo(typesInfo);
        CalculatedPersistentAliasHelper.CustomizeTypesInfo(typesInfo);
    }
    //...
}

After formatting a FullAddress property using the specified format, the property names that are enclosed in curly brackets will be replaced with the current object’s property values (see the image above).
使用指定格式格式化FullAddress属性后,括在大括号中的属性名称将替换为当前对象的属性值(参见上图)。

Analogous to the Address class’ FullAddressFormat property, the Person class’ FullNameFormat property is set to the defaultFullNameFormat constant, which is the following: “{FirstName} {MiddleName} {LastName}”. The FullNamePersistentAlias property is set to the defaultFullNamePersistentAlias constant, which is the following: “concat(FirstName, MiddleName, LastName)”. As the SetFullNameFormat method is static, you are free to call it where required. For instance, you can use the value specified in the configuration file as demonstrated in the code above.
类似于Address类的FullAddressFormat属性,Person类的FullNameFormat属性设置为defaultFullNameFormat常量,如下所示:{FirstName}{MiddleName}{LastName}。FullNamePersistentAlias属性设置为defaultFullNamePersistentAlias常量,如下所示:conat(FirstName, MiddleName,LastName)。由于SetFullNameFormat方法是静态的,您可以在需要时调用它。例如,您可以使用配置文件中指定的值,如上面的代码所示。

Note
In the Main Demo, you can set a custom format for the FullName property in the common module’s constructor, as described above.
在Main Demo中,您可以为公共模块的构造函数中的FullName属性设置自定义格式,如上所述。

When implementing business class properties whose values require formatting, introduce static properties like FullAddressFormat and FullNameFormat, so that anyone using your business class can modify the formatting. The following code can be used as an example:
在实现值需要格式化的业务类属性时,引入FullAddressFormat和FullNameFormat等静态属性,以便任何使用您的业务类的人都可以修改格式。以下代码可以作为示例:

C# 
public class SampleAddress : BaseObject {
    private const string defaultFullAddressFormat = "{Country.Name}; {StateProvince};" +
       " {City}; {Street}; {ZipPostal}";
    private static string fullAddressFormat = defaultFullAddressFormat;
    public static string FullAddressFormat {
        get { return fullAddressFormat; }
        set {
            fullAddressFormat = value;
            if(string.IsNullOrEmpty(fullAddressFormat)) {
                fullAddressFormat = defaultFullAddressFormat;
            }
        }
    }
    public string FullAddress {
        get {
            return ObjectFormatter.Format(fullAddressFormat, this, 
               EmptyEntriesMode.RemoveDelimiterWhenEntryIsEmpty );
        }
    }
}
这个错误通常是由于访问未定义的属性或对象而引起的。在Vue组件中,当你尝试访问一个未定义的属性时,就会出现这个错误。根据你提供的引用内容,错误可能出现在以下几个地方: 1. 引用中的错误信息"TypeError: Cannot read properties of undefined (reading 'state')"表明在某个地方尝试读取一个未定义的属性'state'。这可能是因为你没有正确地设置或初始化Vue组件的状态。 2. 引用中的错误信息"错误代码:Vue组件中的html: <span>{{ userDefaultAddress.fullAddress }}</span> Vue组件中的计算属性:computed: { ...mapState({ addressInfo: (state) => state.trade.address }), userDefaultAddress() { return this.addressInfo.find((item) => item.isDefault == 1); }, }"表明在Vue组件中尝试访问'userDefaultAddress.fullAddress'属性时出现了错误。这可能是因为你没有正确地设置或初始化'userDefaultAddress'属性。 为了解决这个错误,你可以采取以下步骤: 1. 确保你正确地设置和初始化Vue组件的状态。你可以使用Vue的store来管理组件的状态,并在组件中使用mapState来映射状态。 2. 确保你正确地设置和初始化组件中的属性。在你的例子中,你需要确保'userDefaultAddress'属性被正确地设置和初始化。 3. 检查你的代码中是否有任何拼写错误或语法错误。这些错误可能导致属性或对象未定义。 4. 确保你的数据源中包含所需的属性和对象。在你的例子中,你需要确保'addressInfo'数组中包含所需的属性和对象。 5. 如果你仍然无法解决问题,请检查你的代码中是否有其他地方可能导致这个错误的原因。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值