ExpressionEditor 类

定义

定义一组属性和方法,用来计算设计时与控件属性关联的表达式,并向可视化设计宿主提供表达式编辑器表,以便在表达式编辑器对话框中使用。Defines a set of properties and methods for evaluating an expression that is associated with a control property at design time and to provide an expression editor sheet to the visual design host for use in the expression editor dialog box. 此类为抽象类。This class is abstract.

public ref class ExpressionEditor abstract
public abstract class ExpressionEditor
type ExpressionEditor = class
Public MustInherit Class ExpressionEditor
继承
ExpressionEditor
派生

示例

下面的代码示例演示如何从ExpressionEditor类派生以定义自定义表达式编辑器。The following code example demonstrates how to derive from the ExpressionEditor class to define a custom expression editor.

using System;
using System.Collections;
using System.Collections.Specialized;
using System.CodeDom;
using System.Configuration;
using System.Web.UI.Design;
using System.Web.Compilation;

namespace ExpressionEditorSamples.CS
{
    [ExpressionPrefix("CustomAppSettings")]
    [ExpressionEditor(typeof(ExpressionEditorSamples.CS.CustomAppSettingsEditor))]
    public class CustomAppSettingsBuilder : AppSettingsExpressionBuilder
    {
        // Use the built-in AppSettingsExpressionBuilder class,
        // but associate it with a custom expression editor class.
    }

    public class CustomAppSettingsEditor : System.Web.UI.Design.ExpressionEditor
    {
        public override object EvaluateExpression(string expression, object parseTimeData, Type propertyType, IServiceProvider serviceProvider)
        {
            KeyValueConfigurationCollection customSettings = null;

            if (serviceProvider != null)
            {
                IWebApplication webApp = (IWebApplication)serviceProvider.GetService(typeof(IWebApplication));
                if (webApp != null)
                {
                    Configuration config = webApp.OpenWebConfiguration(true);
                    if (config != null)
                    {
                        AppSettingsSection settingsSection = config.AppSettings;
                        if (settingsSection != null)
                        {
                            customSettings = settingsSection.Settings;
                        }
                    }
                }
            }

            if (customSettings != null)
            {
                return customSettings[expression];
            }

            return expression;
        }
    }
}

注解

可视化设计器宿主 (如Visual Studio 2005Visual Studio 2005) ExpressionEditor使用类向用户显示自定义表达式编辑器表, 然后计算所选表达式的设计时呈现。A visual designer host, such as Visual Studio 2005Visual Studio 2005, uses the ExpressionEditor class to present custom expression editor sheets to the user, and then evaluate the selected expression for design-time rendering.

在 "设计时Expressions属性" 网格中浏览控件的属性时, 可视化设计器将显示一个对话框, 用于为控件属性设置表达式。When you browse the Expressions property for a control in the design-time Properties grid, the visual designer displays a dialog box to set expressions for a control property. 您可以选择基于表达式前缀列表的表达式类型。You can select the expression type based on a list of expression prefixes. 从列表中选择表达式前缀时, 可视化设计器会使用关联ExpressionEditor的和ExpressionEditorSheet对象来设置、计算和转换基于该类型表达式的语法的表达式字符串。When you select an expression prefix from the list, the visual designer uses the associated ExpressionEditor and ExpressionEditorSheet objects to set, evaluate, and convert the expression string based on the syntax for that type of expression. 可视化设计器设置关联控件属性的表达式, 然后使用计算的表达式结果来分配设计图面上呈现的控件属性值。The visual designer sets the expression for the associated control property, and then uses the evaluated expression result to assign control property values that are rendered on the design surface.

静态GetExpressionEditor方法获取与特定表达式前缀或表达式生成器关联的表达式编辑器。The static GetExpressionEditor methods get the expression editor that is associated with a particular expression prefix or expression builder. ExpressionEditor对象ExpressionPrefix的属性返回配置的表达式前缀。The ExpressionPrefix property for an ExpressionEditor object returns the configured expression prefix. EvaluateExpression方法计算输入表达式的字符串。The EvaluateExpression method evaluates an input expression string. 方法返回用于在 "表达式" 对话框中提示输入自定义表达式属性的实现。ExpressionEditorSheet GetExpressionEditorSheetThe GetExpressionEditorSheet method returns the ExpressionEditorSheet implementation that is used to prompt for the custom expression properties in the expressions dialog box.

通常, 若要在设计时支持新的表达式类型, 请定义唯一的表达式前缀, 并ExpressionBuilder提供ExpressionEditor自定义和实现。Typically, to support a new expression type at design time, you define a unique expression prefix and provide custom ExpressionBuilder and ExpressionEditor implementations. 还可以提供自定义ExpressionEditorSheet实现, 该实现定义用于在 "表达式" 对话框中构成表达式的属性。Optionally, you can provide a custom ExpressionEditorSheet implementation that defines properties that are used to form the expression in the expressions dialog box.

表达式前缀标识自定义表达式类型, 并将表达式与表达式生成器和表达式编辑器相关联。The expression prefix identifies the custom expression type and associates an expression with the expression builder and expression editor. 当在页中分析自定义表达式时, 表达式前缀用于创建关联ExpressionBuilder的和ExpressionEditor类的实例。When custom expressions are parsed in a page, the expression prefix is used to create instances of the associated ExpressionBuilder and ExpressionEditor classes. 若要将表达式前缀与表达式生成器和表达式编辑器相关联, 请ExpressionEditorAttributeExpressionPrefixAttribute和属性应用于ExpressionBuilder自定义类, 并在expressionBuildersWeb 配置文件中的元素。To associate an expression prefix with an expression builder and expression editor, apply the ExpressionEditorAttribute and ExpressionPrefixAttribute attributes to the custom ExpressionBuilder class and configure the expression prefix for an expression builder in the expressionBuilders element in the Web configuration file. 前缀不是必需的, 但强烈建议使用。The prefix is not required, but highly recommended.

实施者说明

派生自定义ExpressionEditor类时需要执行以下步骤:The following steps are required in deriving a custom ExpressionEditor class: -重写EvaluateExpression(String, Object, Type, IServiceProvider)方法以在设计时计算自定义表达式类型。- Override the EvaluateExpression(String, Object, Type, IServiceProvider) method to evaluate the custom expression type at design time.

-(可选) 重GetExpressionEditorSheet(String, IServiceProvider)写方法以返回一个自定义类, 该类定义组合以形成自定义表达式的属性。- Optionally, override the GetExpressionEditorSheet(String, IServiceProvider) method to return a custom class that defines properties that are combined to form the custom expression.

-在ExpressionBuilderExpressionEditorAttribute声明上应用属性, 将自定义表达式生成器与派生的表达式编辑器类相关联。- Apply the ExpressionEditorAttribute attribute on the ExpressionBuilder class declaration to associate the custom expression builder with the derived expression editor class.

例如, ResourceExpressionEditor类派生ExpressionEditor自类, 并提供用于在设计时计算资源字符串引用并将资源字符串引用与控件属性关联的实现。For example, the ResourceExpressionEditor class derives from the ExpressionEditor class and provides an implementation for evaluating and associating a resource string reference with a control property at design time. 类与表达式前缀ResourcesResourceExpressionEditor实现相关联。 ResourceExpressionBuilderThe ResourceExpressionBuilder class is associated with the expression prefix Resources and the ResourceExpressionEditor implementation. GetExpressionEditorSheet(String, IServiceProvider) 方法ResourceExpressionEditorSheet返回, 它定义构成资源引用表达式的各个属性。The GetExpressionEditorSheet(String, IServiceProvider) method returns a ResourceExpressionEditorSheet, which defines the individual properties that form a resource reference expression.

构造函数

ExpressionEditor()

初始化 ExpressionEditor 类的新实例。Initializes a new instance of the ExpressionEditor class.

属性

ExpressionPrefix

获取表达式前缀,该前缀标识受表达式编辑器实现支持的表达式字符串。Gets the expression prefix that identifies expression strings that are supported by the expression editor implementation.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
EvaluateExpression(String, Object, Type, IServiceProvider)

计算表达式字符串并提供控件属性的设计时值。Evaluates an expression string and provides the design-time value for a control property.

GetExpressionEditor(String, IServiceProvider)

返回一个与指定的表达式前缀相关联的 ExpressionEditor 实现。Returns an ExpressionEditor implementation that is associated with the specified expression prefix.

GetExpressionEditor(Type, IServiceProvider)

返回一个与指定表达式生成器类型相关联的 ExpressionEditor 实现。Returns an ExpressionEditor implementation that is associated with the specified expression builder type.

GetExpressionEditorSheet(String, IServiceProvider)

返回与当前表达式编辑器相关联的表达式编辑器表。Returns an expression editor sheet that is associated with the current expression editor.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅