-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathShapeConverters.cs
More file actions
101 lines (90 loc) · 2.56 KB
/
ShapeConverters.cs
File metadata and controls
101 lines (90 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Globalization;
using Microsoft.Maui.Controls.Shapes;
namespace Maui.Controls.Sample;
public class StringToPointCollectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string pointsString && !string.IsNullOrWhiteSpace(pointsString))
{
var points = new PointCollection();
var pointPairs = pointsString.Split(' ');
foreach (var pointPair in pointPairs)
{
var coords = pointPair.Split(',');
if (coords.Length == 2 &&
double.TryParse(coords[0], out double x) &&
double.TryParse(coords[1], out double y))
{
points.Add(new Point(x, y));
}
}
return points;
}
return new PointCollection();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is PointCollection points)
{
var pointStrings = new List<string>();
foreach (Point point in points)
{
pointStrings.Add($"{point.X},{point.Y}");
}
return string.Join(" ", pointStrings);
}
return string.Empty;
}
}
public class StringToPathGeometryConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string pathData && !string.IsNullOrWhiteSpace(pathData))
{
try
{
return (Geometry)new PathGeometryConverter().ConvertFromInvariantString(pathData);
}
catch
{
// Return a simple default path if parsing fails
return (Geometry)new PathGeometryConverter().ConvertFromInvariantString("M 10,100 L 100,100");
}
}
return (Geometry)new PathGeometryConverter().ConvertFromInvariantString("M 10,100 L 100,100");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.ToString() ?? string.Empty;
}
}
public class StringToDoubleCollectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string dashArray && !string.IsNullOrWhiteSpace(dashArray))
{
var collection = new DoubleCollection();
var values = dashArray.Split(',');
foreach (var val in values)
{
if (double.TryParse(val.Trim(), out double dashValue))
{
collection.Add(dashValue);
}
}
return collection;
}
return new DoubleCollection();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is DoubleCollection collection)
{
return string.Join(",", collection);
}
return string.Empty;
}
}