48 строки
1.7 KiB
C#
48 строки
1.7 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using Xamarin.Forms.Xaml;
|
|
|
|
namespace Xamarin.Forms
|
|
{
|
|
public class FontSizeConverter : TypeConverter, IExtendedTypeConverter
|
|
{
|
|
[Obsolete("use ConvertFromInvariantString (string, IServiceProvider)")]
|
|
object IExtendedTypeConverter.ConvertFrom(CultureInfo culture, object value, IServiceProvider serviceProvider)
|
|
{
|
|
return ((IExtendedTypeConverter)this).ConvertFromInvariantString(value as string, serviceProvider);
|
|
}
|
|
|
|
object IExtendedTypeConverter.ConvertFromInvariantString(string value, IServiceProvider serviceProvider)
|
|
{
|
|
if (value != null)
|
|
{
|
|
double size;
|
|
if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out size))
|
|
return size;
|
|
NamedSize namedSize;
|
|
if (Enum.TryParse(value, out namedSize))
|
|
{
|
|
Type type;
|
|
var valueTargetProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
|
|
type = valueTargetProvider != null ? valueTargetProvider.TargetObject.GetType() : typeof(Label);
|
|
return Device.GetNamedSize(namedSize, type, false);
|
|
}
|
|
}
|
|
throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(double)));
|
|
}
|
|
|
|
public override object ConvertFromInvariantString(string value)
|
|
{
|
|
if (value != null)
|
|
{
|
|
double size;
|
|
if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out size))
|
|
return size;
|
|
NamedSize namedSize;
|
|
if (Enum.TryParse(value, out namedSize))
|
|
return Device.GetNamedSize(namedSize, typeof(Label), false);
|
|
}
|
|
throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(double)));
|
|
}
|
|
}
|
|
} |