.NET支持枚举的FlagsAttribute,表示“可以将枚举视为位字段”。
使用此[Flags]属性的枚举示例为System.Windows.Forms.Keys
这是我的Spring.NET对象定义:
<object id="command.paste" type="MyNamespace.PasteClipboardCommand, MyProject">
<property name="Title" value="Paste into Folder" />
<property name="ShortcutKeys" value="P" />
</object>
这会将Keys类型的ShortcutKeys设置为键P.没有问题。但是,由于这是一个快捷方式,我想指定像Control + P这样的快捷键。
如果我在代码中连接它,那将是:
command.ShortcutKeys = Keys.Control | Keys.P;
所以,也许我尝试这个有点天真:
<property name="ShortcutKeys" value="Control|P" />
这失败了,因为EnumConverter只能解析/转换单个枚举的字符串表示。完整的堆栈跟踪可在下面找到。
更新
显然KeysConverter知道如何处理这个:
(new KeysConverter()).ConvertFrom(null, null, "Control+P");
不幸的是,EnumConverter不支持这个吗?
附加Stacktrace:
System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are:
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.String] to required type [System.Windows.Forms.Keys] for property 'ShortcutKeys'., Inner Exception: System.FormatException: Control|P is not a valid value for Keys. ---> System.ArgumentException: Requested value 'Control|P' was not found.
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
at Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName)] ---> Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'command.builder.paste' defined in 'file [....\commands.xml] line 39' : Error setting property values: PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are:
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.String] to required type [System.Windows.Forms.Keys] for property 'ShortcutKeys'., Inner Exception: System.FormatException: Control|P is not a valid value for Keys. ---> System.ArgumentException: Requested value 'Control|P' was not found.
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
at Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName)] ---> PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are:
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.String] to required type [System.Windows.Forms.Keys] for property 'ShortcutKeys'., Inner Exception: System.FormatException: Control|P is not a valid value for Keys. ---> System.ArgumentException: Requested value 'Control|P' was not found.
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
at Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName)]
答案 0 :(得分:4)
如果查看Spring.NET文档5.11 Configuration of IApplicationContext,您可以看到如何为给定类型添加特定类型转换器的示例。
在app.config中为类型键添加KeysConverter就足够了:
<converter for="System.Windows.Forms.Keys, System.Windows.Forms" type="System.Windows.Forms.KeysConverter, System.Windows.Forms"/>
现在可以像这样指定enumvalue /快捷方式:
<property name="ShortcutKeys" value="Control+P" />
答案 1 :(得分:1)
尝试使用SpEL(Spring.NET表达式语言): http://www.springframework.net/doc-latest/reference/html/expressions.html
<property name="ShortcutKeys" expression="Keys.Control or Keys.P" />
或
<property name="ShortcutKeys" expression="T(System.Windows.Forms.Keys).Control or T(System.Windows.Forms.Keys).P" />
答案 2 :(得分:1)
<property name="ShortcutKeys" value="Control, P" />