命令执行后,可以把命令的执行结果保持到变量里,以便后续的命令或逻辑使用。
可以通过实现ResultToPropertyAttribute来实现此效果。
示例代码:
注意:标注 ResultToProperty 的属性类型必须是 string。 推荐给属性添加默认值,以方便用户使用。
public class MyPluginCommand : Command
{
[DisplayName("加数")]
[FormulaProperty]
public object FirstValue { get; set; }
[DisplayName("被加数")]
[FormulaProperty]
public object LastValue { get; set; }
[ResultToProperty]
[DisplayName("结果保存至变量")]
public string Result { get; set; } = "结果";
}
设计器中的效果:
在后续命令编辑公式时,设置的变量可以直接在公式中使用。
在JavaScript 部分,通过Forguncy.CommandHelper.setVariableValue方法设置结果到结果变量。
class MyPluginCommand extends Forguncy.Plugin.CommandBase{
execute() {
const add1 = this.evaluateFormula(this.CommandParam.FirstValue);
const add2 = this.evaluateFormula(this.CommandParam.LastValue);
const res = Number(add1) + Number(add2);
Forguncy.CommandHelper.setVariableValue(this.CommandParam.Result, res);
}
}
Forguncy.Plugin.CommandFactory.registerCommand("MyPlugin.MyPluginCommand, MyPlugin", MyPluginCommand);

