Skip to content

命令属性

此特性为活字格V9.0.100.0新增功能

如果属性表示运行时执行的命令,希望通过命令对话框编辑,可以通过标注CustomCommandObjectAttribute 的方式设置。
注意,标注CustomCommandObjectAttribute的属性类型必须是 object

csharp
using GrapeCity.Forguncy.Commands;
using System.ComponentModel;
using System.Threading.Tasks;

namespace MyPlugin
{
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [CustomCommandObject]
        [DisplayName("自定义子命令")]
        public object CustomSubCommand { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            await dataContext.ExecuteCustomCommandObjectAsync(this.CustomSubCommand, null);
            return new ExecuteResult();
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
}

在设计器中效果如下

1693552175243-c2c72e3b-b9d4-4874-9301-e374993d7e5c.png

执行效果
1693552658625-637ca602-9386-453c-8d13-b0ef4462fa89.png

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

  1. 支持上下文参数
    1. 设置CustomCommandObjectAttribute 的 InitParamValues 和 InitParamProperties 属性
    2. 代码
csharp
    public class MyPluginCellType : CellType
    {
        [CustomCommandObject(InitParamProperties = "x|y", InitParamValues = "X坐标|Y坐标")]
        [DisplayName("双击命令")]
        public object DoubleClickCommand { get; set; }
    }
3. 设计器效果  

1693552724522-bd521f23-8e41-4042-be9f-8d7f36e5543e.png 4. C# 添加上下文参数处理

javascript
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [CustomCommandObject(InitParamProperties = "x|y", InitParamValues = "X坐标|Y坐标")]
        [DisplayName("自定义子命令")]
        public object CustomSubCommand { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var param = new Dictionary<string, object>
            {
                {"X坐标",1 },
                {"Y坐标",2 }
            };
            await dataContext.ExecuteCustomCommandObjectAsync(this.CustomSubCommand, param, "自定义子命令");
            return new ExecuteResult();
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
5. 执行效果  

1693553139345-7c354003-0251-4288-85e8-2f2fee649a90.png 2. 上下文参数支持添加描述, 1. 设置CustomCommandObjectAttribute 的 ParameterDescriptions 属性 2. 代码

csharp
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [CustomCommandObject(InitParamProperties = "x|y", InitParamValues = "X坐标|Y坐标",
            ParameterDescriptions = "x:从坐标圆点水平向右的距离,单位为像素|y:从坐标原点垂直向下的距离,单位为像素")]
        [DisplayName("自定义子命令")]
        public object CustomSubCommand { get; set; }

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var param = new Dictionary<string, object>
            {
                {"X坐标",1 },
                {"Y坐标",2 }
            };
            await dataContext.ExecuteCustomCommandObjectAsync(this.CustomSubCommand, param, "自定义子命令");
            return new ExecuteResult();
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }
3. 设计器效果  

1693553278673-138c0e93-48b8-499b-8578-c8a3f558a7e8.png 4. 说明 1. 添加适当的描述可以帮助用户更好的理解上下文变量的意义 2. 可以只给部分上下文变量添加注释

此属性需要活字格版本号大于9.0.100.0

更新: 2023-11-02 09:49:00
原文: https://www.yuque.com/robert-bh51n/ea8l6c/vh0lo1dgc629zdvh