增加StringEnumConverter

This commit is contained in:
ww-rm
2025-03-30 17:26:17 +08:00
parent 404f255f14
commit bbbb02500f
3 changed files with 44 additions and 15 deletions

View File

@@ -19,7 +19,7 @@ namespace SpineViewer.Exporter.Implementations.ExportArgs
/// <summary> /// <summary>
/// 文件名后缀 /// 文件名后缀
/// </summary> /// </summary>
[TypeConverter(typeof(SFMLImageFileSuffixConverter))] [TypeConverter(typeof(StringEnumConverter)), StringEnumConverter.StandardValues(".png", ".jpg", ".tga", ".bmp")]
[Category("[2] "), DisplayName(""), Description("")] [Category("[2] "), DisplayName(""), Description("")]
public string FileSuffix { get; set; } = ".png"; public string FileSuffix { get; set; } = ".png";
} }

View File

@@ -10,20 +10,6 @@ using System.Threading.Tasks;
namespace SpineViewer.Exporter 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 public class SFMLColorConverter : ExpandableObjectConverter
{ {
private class SFMLColorPropertyDescriptor : SimplePropertyDescriptor private class SFMLColorPropertyDescriptor : SimplePropertyDescriptor

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Globalization; using System.Globalization;
@@ -45,4 +46,46 @@ namespace SpineViewer
return base.ConvertFrom(context, culture, value); return base.ConvertFrom(context, culture, value);
} }
} }
public class StringEnumConverter : StringConverter
{
/// <summary>
/// 字符串标准值列表属性
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class StandardValuesAttribute : Attribute
{
/// <summary>
/// 标准值列表
/// </summary>
public ReadOnlyCollection<string> StandardValues { get; private set; }
private readonly List<string> standardValues = [];
/// <summary>
/// 字符串标准值列表
/// </summary>
/// <param name="values">允许的字符串标准值</param>
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<StandardValuesAttribute>().FirstOrDefault();
StandardValuesCollection result;
if (attribute != null)
result = new StandardValuesCollection(attribute.StandardValues);
else
result = new StandardValuesCollection(Array.Empty<string>());
return result;
}
}
} }