Browse Source

Adds DateTimeTypeConverter

pull/582/head
Seeker1437 10 years ago
parent
commit
0941ba7afa
  1. 3
      src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj
  2. 23
      src/Markup/Avalonia.Markup.Xaml/Context/AvaloniaTypeFeatureProvider.cs
  3. 58
      src/Markup/Avalonia.Markup.Xaml/Converters/DateTimeTypeConverter.cs

3
src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
@ -59,6 +59,7 @@
<Compile Include="Converters\ColorTypeConverter.cs" /> <Compile Include="Converters\ColorTypeConverter.cs" />
<Compile Include="Converters\ColumnDefinitionsTypeConverter.cs" /> <Compile Include="Converters\ColumnDefinitionsTypeConverter.cs" />
<Compile Include="Converters\CursorTypeConverter.cs" /> <Compile Include="Converters\CursorTypeConverter.cs" />
<Compile Include="Converters\DateTimeTypeConverter.cs" />
<Compile Include="Converters\GeometryTypeConverter.cs" /> <Compile Include="Converters\GeometryTypeConverter.cs" />
<Compile Include="Converters\GridLengthTypeConverter.cs" /> <Compile Include="Converters\GridLengthTypeConverter.cs" />
<Compile Include="Converters\KeyGestureConverter.cs" /> <Compile Include="Converters\KeyGestureConverter.cs" />

23
src/Markup/Avalonia.Markup.Xaml/Context/AvaloniaTypeFeatureProvider.cs

@ -1,15 +1,6 @@
// Copyright (c) The Avalonia Project. All rights reserved. // Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using OmniXaml;
using OmniXaml.Builder;
using OmniXaml.TypeConversion;
using OmniXaml.TypeConversion.BuiltInConverters;
using OmniXaml.Typing;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
@ -19,13 +10,22 @@ using Avalonia.Media;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Metadata; using Avalonia.Metadata;
using Avalonia.Styling; using Avalonia.Styling;
using OmniXaml;
using OmniXaml.Builder;
using OmniXaml.TypeConversion;
using OmniXaml.TypeConversion.BuiltInConverters;
using OmniXaml.Typing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using OmniMetadata = OmniXaml.Typing.Metadata; using OmniMetadata = OmniXaml.Typing.Metadata;
namespace Avalonia.Markup.Xaml.Context namespace Avalonia.Markup.Xaml.Context
{ {
public class AvaloniaTypeFeatureProvider : ITypeFeatureProvider public class AvaloniaTypeFeatureProvider : ITypeFeatureProvider
{ {
private Dictionary<Type, OmniMetadata> _metadata = private Dictionary<Type, OmniMetadata> _metadata =
new Dictionary<Type, OmniMetadata>(); new Dictionary<Type, OmniMetadata>();
private Dictionary<Type, TypeConverterRegistration> _typeConverters = private Dictionary<Type, TypeConverterRegistration> _typeConverters =
@ -100,7 +100,7 @@ namespace Avalonia.Markup.Xaml.Context
result = property.Name; result = property.Name;
} }
if (result != null) if (result != null)
{ {
return result; return result;
@ -154,6 +154,7 @@ namespace Avalonia.Markup.Xaml.Context
RegisterTypeConverter(typeof(Color), new ColorTypeConverter()); RegisterTypeConverter(typeof(Color), new ColorTypeConverter());
RegisterTypeConverter(typeof(Classes), new ClassesTypeConverter()); RegisterTypeConverter(typeof(Classes), new ClassesTypeConverter());
RegisterTypeConverter(typeof(ColumnDefinitions), new ColumnDefinitionsTypeConverter()); RegisterTypeConverter(typeof(ColumnDefinitions), new ColumnDefinitionsTypeConverter());
RegisterTypeConverter(typeof(DateTime), new DateTimeTypeConverter());
RegisterTypeConverter(typeof(Geometry), new GeometryTypeConverter()); RegisterTypeConverter(typeof(Geometry), new GeometryTypeConverter());
RegisterTypeConverter(typeof(GridLength), new GridLengthTypeConverter()); RegisterTypeConverter(typeof(GridLength), new GridLengthTypeConverter());
RegisterTypeConverter(typeof(KeyGesture), new KeyGestureConverter()); RegisterTypeConverter(typeof(KeyGesture), new KeyGestureConverter());

58
src/Markup/Avalonia.Markup.Xaml/Converters/DateTimeTypeConverter.cs

@ -0,0 +1,58 @@
using OmniXaml.TypeConversion;
using System;
using System.Globalization;
namespace Avalonia.Markup.Xaml.Converters
{
public class DateTimeTypeConverter : ITypeConverter
{
public object ConvertFrom(IValueContext context, CultureInfo culture, object value)
{
if (culture == null)
{
throw new ArgumentNullException("culture");
}
if (value == null)
{
throw new ArgumentNullException("value");
}
DateTimeFormatInfo dateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
DateTime d = DateTime.ParseExact(value.ToString(), dateTimeFormatInfo.ShortDatePattern, culture);
return d;
}
public object ConvertTo(IValueContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (culture == null)
{
throw new ArgumentNullException("culture");
}
DateTime? d = value as DateTime?;
if (!d.HasValue || destinationType != typeof(string))
{
throw new NotSupportedException();
}
DateTimeFormatInfo dateTimeFormatInfo = (DateTimeFormatInfo)culture.GetFormat(typeof(DateTimeFormatInfo));
return d.Value.ToString(dateTimeFormatInfo.ShortDatePattern, culture);
}
public bool CanConvertTo(IValueContext context, Type destinationType)
{
return destinationType == typeof(string);
}
public bool CanConvertFrom(IValueContext context, Type sourceType)
{
return sourceType == typeof(string);
}
}
}
Loading…
Cancel
Save