40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
||
using System.Globalization;
|
||
using System.Linq;
|
||
using Avalonia.Data.Converters;
|
||
|
||
namespace DispenserUI.ViewModels.Converter;
|
||
|
||
public class StringToDoubleArrayConverter : IValueConverter
|
||
{
|
||
public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture)
|
||
{
|
||
// 从double[] 转换到 string
|
||
if (value is double[] doubleArray) return doubleArray;
|
||
|
||
return null;
|
||
}
|
||
|
||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||
{
|
||
// 从string转换到 double[]
|
||
if (value is string str)
|
||
try
|
||
{
|
||
str = str.Replace(" ", ",").Replace(",", ",");
|
||
var doubleArray = str.Split(',')
|
||
.Where(s => s.Length > 0)
|
||
.Select(s => double.Parse(s, culture))
|
||
.ToArray();
|
||
return doubleArray;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Console.WriteLine(e.StackTrace);
|
||
// 处理转换异常
|
||
return Array.Empty<double>();
|
||
}
|
||
|
||
return null;
|
||
}
|
||
} |