Added FileSizeNameConverter
This commit is contained in:
@@ -18,4 +18,4 @@ public class BooleanToPasswordCharConverter :
|
|||||||
|
|
||||||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using Avalonia.Data.Converters;
|
||||||
|
using Avalonia.Markup.Xaml;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
namespace Toolkit.UI.Avalonia;
|
||||||
|
|
||||||
|
public class FileSizeNameConverter :
|
||||||
|
MarkupExtension,
|
||||||
|
IValueConverter
|
||||||
|
{
|
||||||
|
public override object ProvideValue(IServiceProvider serviceProvider) =>
|
||||||
|
this;
|
||||||
|
|
||||||
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is long fileSize)
|
||||||
|
{
|
||||||
|
string[] sizeSuffixes = ["bytes", "KB", "MB", "GB", "TB", "PB"];
|
||||||
|
|
||||||
|
int order = 0;
|
||||||
|
double adjustedSize = fileSize;
|
||||||
|
|
||||||
|
while (adjustedSize >= 1024 && order < sizeSuffixes.Length - 1)
|
||||||
|
{
|
||||||
|
order++;
|
||||||
|
adjustedSize /= 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $"{adjustedSize:0.##} {sizeSuffixes[order]}";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "0 bytes";
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) =>
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user