Dispenser/DispenserUI/ViewModels/Converter/StringToDoubleArrayConverte...

40 lines
1.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}