From 05d8b84ce3e4f30c6c5c643ce6cbf841c24121d5 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Tue, 3 Jan 2023 23:21:53 +0800 Subject: [PATCH] feat: Add GeometryConverter --- src/Avalonia.Base/Media/Geometry.cs | 33 ++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Media/Geometry.cs b/src/Avalonia.Base/Media/Geometry.cs index 2019f54c70..b1fea53289 100644 --- a/src/Avalonia.Base/Media/Geometry.cs +++ b/src/Avalonia.Base/Media/Geometry.cs @@ -1,11 +1,14 @@ using System; using Avalonia.Platform; +using System.ComponentModel; +using System.Globalization; namespace Avalonia.Media { /// /// Defines a geometric shape. - /// + /// + [TypeConverter(typeof(GeometryConverter))] public abstract class Geometry : AvaloniaObject { /// @@ -199,4 +202,32 @@ namespace Avalonia.Media return new CombinedGeometry(combineMode, geometry1, geometry2, transform); } } + + public class GeometryConverter : TypeConverter + { + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + if (sourceType == typeof(string)) + { + return true; + } + return base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) + { + if (value is null) + { + throw GetConvertFromException(value); + } + string source = value as string; + + if (source != null) + { + return Geometry.Parse(source); + } + + return base.ConvertFrom(context, culture, value); + } + } }