From bbbb02500f231fa64c1df195b46487c8cf14ea38 Mon Sep 17 00:00:00 2001 From: ww-rm Date: Sun, 30 Mar 2025 17:26:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0StringEnumConverter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExportArgs/FrameSequenceExportArgs.cs | 2 +- SpineViewer/Exporter/TypeConverter.cs | 14 ------ SpineViewer/TypeConverter.cs | 43 +++++++++++++++++++ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/SpineViewer/Exporter/Implementations/ExportArgs/FrameSequenceExportArgs.cs b/SpineViewer/Exporter/Implementations/ExportArgs/FrameSequenceExportArgs.cs index 8f7208c..2d3a3c4 100644 --- a/SpineViewer/Exporter/Implementations/ExportArgs/FrameSequenceExportArgs.cs +++ b/SpineViewer/Exporter/Implementations/ExportArgs/FrameSequenceExportArgs.cs @@ -19,7 +19,7 @@ namespace SpineViewer.Exporter.Implementations.ExportArgs /// /// 文件名后缀 /// - [TypeConverter(typeof(SFMLImageFileSuffixConverter))] + [TypeConverter(typeof(StringEnumConverter)), StringEnumConverter.StandardValues(".png", ".jpg", ".tga", ".bmp")] [Category("[2] 帧序列参数"), DisplayName("文件名后缀"), Description("帧文件的后缀,同时决定帧图像格式")] public string FileSuffix { get; set; } = ".png"; } diff --git a/SpineViewer/Exporter/TypeConverter.cs b/SpineViewer/Exporter/TypeConverter.cs index 7fe0bb5..d43dd67 100644 --- a/SpineViewer/Exporter/TypeConverter.cs +++ b/SpineViewer/Exporter/TypeConverter.cs @@ -10,20 +10,6 @@ using System.Threading.Tasks; namespace SpineViewer.Exporter { - public class SFMLImageFileSuffixConverter : StringConverter - { - private readonly string[] supportedFileSuffix = [".png", ".jpg", ".tga", ".bmp"]; - - public override bool GetStandardValuesSupported(ITypeDescriptorContext? context) => true; - - public override bool GetStandardValuesExclusive(ITypeDescriptorContext? context) => true; - - public override StandardValuesCollection? GetStandardValues(ITypeDescriptorContext? context) - { - return new StandardValuesCollection(supportedFileSuffix); - } - } - public class SFMLColorConverter : ExpandableObjectConverter { private class SFMLColorPropertyDescriptor : SimplePropertyDescriptor diff --git a/SpineViewer/TypeConverter.cs b/SpineViewer/TypeConverter.cs index 8f5e156..6d25d2a 100644 --- a/SpineViewer/TypeConverter.cs +++ b/SpineViewer/TypeConverter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; @@ -45,4 +46,46 @@ namespace SpineViewer return base.ConvertFrom(context, culture, value); } } + + public class StringEnumConverter : StringConverter + { + /// + /// 字符串标准值列表属性 + /// + [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] + public class StandardValuesAttribute : Attribute + { + /// + /// 标准值列表 + /// + public ReadOnlyCollection StandardValues { get; private set; } + private readonly List standardValues = []; + + /// + /// 字符串标准值列表 + /// + /// 允许的字符串标准值 + public StandardValuesAttribute(params string[] values) + { + standardValues.AddRange(values); + StandardValues = standardValues.AsReadOnly(); + } + } + + public override bool GetStandardValuesSupported(ITypeDescriptorContext? context) => true; + + public override bool GetStandardValuesExclusive(ITypeDescriptorContext? context) => true; + + public override StandardValuesCollection? GetStandardValues(ITypeDescriptorContext? context) + { + // 查找属性上的 StandardValuesAttribute + var attribute = context?.PropertyDescriptor?.Attributes.OfType().FirstOrDefault(); + StandardValuesCollection result; + if (attribute != null) + result = new StandardValuesCollection(attribute.StandardValues); + else + result = new StandardValuesCollection(Array.Empty()); + return result; + } + } }