Skip to content

公式属性

如果属性值需要依赖公式的计算结果动态变化,可以通过标注FormulaPropertyAttribute 的方式设置。
注意,标注FormulaPropertyAttribute的属性类型必须是 object
为支持公式属性,ExecuteAsync执行函数中也需要相应的处理,可以通过EvaluateFormulaAsync方法计算公式的值

csharp
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty]
        public object MyProperty { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var propCalcedValue = await dataContext.EvaluateFormulaAsync(this.MyProperty);
            return new ExecuteResult() { Message = propCalcedValue?.ToString() };
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }

在设计器中效果如下

1669595407047-a2a5ce87-ea92-4200-bb45-0104e4271fd7.png

如果需要更细致的控制,可以通过FormulaPropertyAttribute的其他属性来控制

  1. 提供备选列表
    1. 设置FormulaPropertyAttribute 的 RecommendedValues 属性
      使用“|”分隔多个候选项
    2. 代码
csharp
    [Designer("MyPlugin.Designer.MyPluginServerCommandDesigner, MyPlugin")]
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty(RecommendedValues = "学生|教师|工人")]
        public object MyProperty { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var propCalcedValue = await dataContext.EvaluateFormulaAsync(this.MyProperty);
            return new ExecuteResult() { Message = propCalcedValue?.ToString() };
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
3. 效果  

1670153142961-9be32a1f-0c32-4b49-ad09-16ab8d35e8e1.png 2. 支持输入多行文本 1. 设置FormulaPropertyAttribute 的 AcceptsReturn 属性 2. 代码

csharp
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty(AcceptsReturn = true)]
        public object MyProperty { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var propCalcedValue = await dataContext.EvaluateFormulaAsync(this.MyProperty);
            return new ExecuteResult() { Message = propCalcedValue?.ToString() };
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
3. 效果  

1670153238174-8e54fa2a-8404-4287-b690-67ffb4579a0e.png 3. 支持多语言 1. 所有公式属性,默认会开启多语言支持,可以通过设置FormulaPropertyAttribute 的 CanSelectResource 属性关闭多语言支持。 2. 代码

csharp
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty(CanSelectResource = false)]
        public object MyProperty { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var propCalcedValue = await dataContext.EvaluateFormulaAsync(this.MyProperty);
            return new ExecuteResult() { Message = propCalcedValue?.ToString() };
        }
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
3. 此特性为 10.0.0.0 新增

更新: 2024-02-17 10:07:41
原文: https://www.yuque.com/robert-bh51n/ea8l6c/uhm1eb3z9wdg58dn