默认情况下,如果一个属性的类型是 double 那么这个属性会被自动识别为小数属性,不需要做任何额外的事情。
public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync { public double MyProperty { get; set; } public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext) { return new ExecuteResult(); } public override CommandScope GetCommandScope() { return CommandScope.ExecutableInServer; } }
在设计器中效果如下:
如果需要更细致的控制,需要使用DoublePropertyAttribute标注来控制。
注意,标注DoublePropertyAttribute的属性类型必须是 double 或者 double?。
- 控制最大值最小值
- 设置DoubleProperty的 Min 和 Max 属性。
代码:
public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync { [DoubleProperty(Min = 0, Max = 10)] public double MyProperty { get; set; } public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext) { return new ExecuteResult(); } public override CommandScope GetCommandScope() { return CommandScope.ExecutableInServer; } }}
- 效果:
在设计器中MyProperty属性只能输入 1 到 10 之间的小数。 - 其他说明:
如果没有设置最大值最小值,则最小值是 -1.7976931348623157E+308, 最大值是 1.7976931348623157E+308。
- 控制属性值可空
- 设置DoubleProperty 的 AllowNull属性,Watermark属性可选。
代码:
public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync { [DoubleProperty(AllowNull = true, Watermark = "无限制")] public double? MyProperty { get; set; } public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext) { return new ExecuteResult(); } public override CommandScope GetCommandScope() { return CommandScope.ExecutableInServer; } }
- 效果:
- 注意:
属性的类型需要声明为 double? 而不是 double (double 后边有一个问号)。
- 设置DoubleProperty 的 AllowNull属性,Watermark属性可选。