Browse Source

comment some omnixaml converters

pull/916/head
donandren 10 years ago
committed by Andrey Kunchev
parent
commit
03d8dea955
  1. 45
      src/Markup/Avalonia.Markup.Xaml/Converters/BitmapTypeConverter.cs
  2. 2
      src/Markup/Avalonia.Markup.Xaml/Converters/UriTypeConverter.cs
  3. 46
      src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaDefaultTypeConverters.cs

45
src/Markup/Avalonia.Markup.Xaml/Converters/BitmapTypeConverter.cs

@ -3,12 +3,53 @@
using System; using System;
using System.Globalization; using System.Globalization;
using OmniXaml.TypeConversion;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Platform; using Avalonia.Platform;
namespace Avalonia.Markup.Xaml.Converters namespace Avalonia.Markup.Xaml.Converters
{ {
#if !OMNIXAML
using Portable.Xaml.ComponentModel;
public class BitmapTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var uri = new Uri((string)value, UriKind.RelativeOrAbsolute);
var baseUri = GetBaseUri(context, value as string);
var scheme = uri.IsAbsoluteUri ? uri.Scheme : "file";
switch (scheme)
{
case "file":
return new Bitmap((string)value);
default:
var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return new Bitmap(assets.Open(uri, baseUri));
}
}
private Uri GetBaseUri(ITypeDescriptorContext context, string value)
{
return (Uri)TypeDescriptor.GetConverter(typeof(Uri)).ConvertFrom(value);
//TODO:
//object result;
//context.ParsingDictionary.TryGetValue("Uri", out result);
//return result as Uri;
}
}
#else
using OmniXaml.TypeConversion;
public class BitmapTypeConverter : ITypeConverter public class BitmapTypeConverter : ITypeConverter
{ {
public bool CanConvertFrom(IValueContext context, Type sourceType) public bool CanConvertFrom(IValueContext context, Type sourceType)
@ -31,6 +72,7 @@ namespace Avalonia.Markup.Xaml.Converters
{ {
case "file": case "file":
return new Bitmap((string)value); return new Bitmap((string)value);
default: default:
var assets = AvaloniaLocator.Current.GetService<IAssetLoader>(); var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return new Bitmap(assets.Open(uri, baseUri)); return new Bitmap(assets.Open(uri, baseUri));
@ -49,4 +91,5 @@ namespace Avalonia.Markup.Xaml.Converters
return result as Uri; return result as Uri;
} }
} }
#endif
} }

2
src/Markup/Avalonia.Markup.Xaml/Converters/UriTypeConverter.cs

@ -7,6 +7,7 @@ using OmniXaml.TypeConversion;
namespace Avalonia.Markup.Xaml.Converters namespace Avalonia.Markup.Xaml.Converters
{ {
#if OMNIXAML
public class UriTypeConverter : ITypeConverter public class UriTypeConverter : ITypeConverter
{ {
public bool CanConvertFrom(IValueContext context, Type sourceType) public bool CanConvertFrom(IValueContext context, Type sourceType)
@ -29,4 +30,5 @@ namespace Avalonia.Markup.Xaml.Converters
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
#endif
} }

46
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaDefaultTypeConverters.cs

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Reflection; using System.Reflection;
using Avalonia.Markup.Xaml.Converters; using Avalonia.Markup.Xaml.Converters;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Styling; using Avalonia.Styling;
using Portable.Xaml.ComponentModel;
namespace Avalonia.Markup.Xaml.PortableXaml namespace Avalonia.Markup.Xaml.PortableXaml
{ {
@ -12,6 +14,7 @@ namespace Avalonia.Markup.Xaml.PortableXaml
{ {
private static Dictionary<Type, Type> _defaultConverters = new Dictionary<Type, Type>() private static Dictionary<Type, Type> _defaultConverters = new Dictionary<Type, Type>()
{ {
//avalonia default converters
{ typeof(IBitmap), typeof(BitmapTypeConverter)}, { typeof(IBitmap), typeof(BitmapTypeConverter)},
{ typeof(IBrush), typeof(BrushTypeConverter) }, { typeof(IBrush), typeof(BrushTypeConverter) },
//{ typeof(Color), typeof(ColorTypeConverter) }, //{ typeof(Color), typeof(ColorTypeConverter) },
@ -31,7 +34,7 @@ namespace Avalonia.Markup.Xaml.PortableXaml
//{ typeof(RowDefinitions), typeof(RowDefinitionsTypeConverter) }, //{ typeof(RowDefinitions), typeof(RowDefinitionsTypeConverter) },
//{ typeof(Size), typeof(SizeTypeConverter) }, //{ typeof(Size), typeof(SizeTypeConverter) },
{ typeof(Selector), typeof(SelectorTypeConverter)}, { typeof(Selector), typeof(SelectorTypeConverter)},
//{ typeof(SolidColorBrush), typeof(BrushTypeConverter) }, { typeof(SolidColorBrush), typeof(BrushTypeConverter) },
//{ typeof(Thickness), typeof(ThicknessTypeConverter) }, //{ typeof(Thickness), typeof(ThicknessTypeConverter) },
//{ typeof(TimeSpan), typeof(TimeSpanTypeConverter) }, //{ typeof(TimeSpan), typeof(TimeSpanTypeConverter) },
//{ typeof(Uri), typeof(Converters.UriTypeConverter) }, //{ typeof(Uri), typeof(Converters.UriTypeConverter) },
@ -44,26 +47,33 @@ namespace Avalonia.Markup.Xaml.PortableXaml
{ {
Type converterType; Type converterType;
Type curType = type; if (_defaultConverters.TryGetValue(type, out converterType))
while (curType != null)
{ {
if (_defaultConverters.TryGetValue(curType, out converterType)) return converterType;
{ }
if (curType != type)
{ //TODO: probably a smarter way to handle types
_defaultConverters[curType] = converterType; //is it needed ??
} //Type curType = type;
//while (curType != null)
//{
// if (_defaultConverters.TryGetValue(curType, out converterType))
// {
// if (curType != type)
// {
// _defaultConverters[curType] = converterType;
// }
// return converterType;
// }
// else
// {
// _defaultConverters[curType] = null;
// }
// curType = curType.GetTypeInfo().BaseType;
//}
return converterType;
}
else
{
_defaultConverters[curType] = null;
}
curType = curType.GetTypeInfo().BaseType;
}
return null; return null;
} }

Loading…
Cancel
Save