如果属性值需要依赖公式的计算结果动态变化,可以通过标注FormulaPropertyAttribute 的方式设置。
注意,标注FormulaPropertyAttribute的属性类型必须是 object.
为支持公式属性,ExecuteAsync执行函数中也需要相应的处理,可以通过EvaluateFormulaAsync方法计算公式的值。
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; } }
在设计器中效果如下:
如果需要更细致的控制,可以通过FormulaPropertyAttribute的其他属性来控制。
- 提供备选列表
- 设置FormulaPropertyAttribute 的 RecommendedValues 属性,使用“|”分隔多个候选项。
代码:
[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; } }
- 效果:
- 支持输入多行文本。
- 设置FormulaPropertyAttribute 的 AcceptsReturn 属性。
代码:
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; } }
- 效果:
- 设置FormulaPropertyAttribute 的 AcceptsReturn 属性。