mirror of
https://github.com/jayfunc/BetterLyrics.git
synced 2026-01-12 10:54:55 +08:00
46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using Microsoft.UI.Xaml.Data;
|
|
using System;
|
|
|
|
namespace BetterLyrics.WinUI3.Converter
|
|
{
|
|
public partial class MillisecondsToFormattedTimeConverter : IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, string language)
|
|
{
|
|
double? milliseconds = null;
|
|
|
|
if (value is int iVal) milliseconds = iVal;
|
|
else if (value is double dVal) milliseconds = dVal;
|
|
else if (value is long lVal) milliseconds = lVal;
|
|
|
|
if (milliseconds.HasValue)
|
|
{
|
|
var ts = TimeSpan.FromMilliseconds(milliseconds.Value);
|
|
|
|
string? format = parameter?.ToString();
|
|
|
|
if (string.IsNullOrEmpty(format))
|
|
{
|
|
format = @"mm\:ss\.fff";
|
|
}
|
|
|
|
try
|
|
{
|
|
return ts.ToString(format);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
return ts.ToString();
|
|
}
|
|
}
|
|
|
|
return value?.ToString() ?? "";
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|