From 97302556019d00a63ebe1a30fb47f39c507afb6b Mon Sep 17 00:00:00 2001 From: donandren Date: Thu, 2 Mar 2017 22:16:46 +0200 Subject: [PATCH] fixed a problem with Portable.xaml siimple types are not resolved when used with clr-namespace --- .../PortableXaml/AvaloniaXamlSchemaContext.cs | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlSchemaContext.cs b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlSchemaContext.cs index 818aa2e43b..9b08d726dc 100644 --- a/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlSchemaContext.cs +++ b/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlSchemaContext.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using Avalonia.Markup.Xaml.Context; @@ -54,12 +55,61 @@ namespace Avalonia.Markup.Xaml.PortableXaml if (type == null) { - return null; + //let's try the simple types + //in Portable xaml xmlns:sys='clr-namespace:System;assembly=mscorlib' + //and sys:Double is not resolved + return ResolveSimpleTypeName(xmlNamespace, xmlLocalName); } return GetXamlType(type); } + #region Workaround for bug in Portablexaml system types like double,int etc ... + + private static Type[] _simpleTypes = new Type[] + { + typeof(bool), + typeof(byte), + typeof(char), + typeof(decimal), + typeof(double), + typeof(Int16), + typeof(Int32), + typeof(Int64), + typeof(float), + typeof(string), + typeof(TimeSpan), + typeof(Uri), + }; + + private static Dictionary, XamlType> _simpleXamlTypes; + + private static XamlType ResolveSimpleTypeName(string xmlNamespace, string xmlLocalName) + { + if (_simpleXamlTypes == null) + { + _simpleXamlTypes = new Dictionary, XamlType>(); + + foreach (var type in _simpleTypes) + { + string asmName = type.GetTypeInfo().Assembly.GetName().Name; + string ns = $"clr-namespace:{type.Namespace};assembly={asmName}"; + var xamlType = XamlLanguage.AllTypes.First(t => t.UnderlyingType == type); + _simpleXamlTypes.Add(new Tuple(ns, type.Name), xamlType); + } + } + + XamlType result; + + var key = new Tuple(xmlNamespace, xmlLocalName); + + _simpleXamlTypes.TryGetValue(key, out result); + + return result; + } + + #endregion Workaround for bug in Portablexaml system types like double,int etc ... + protected override ICustomAttributeProvider GetCustomAttributeProvider(Type type) => new AvaloniaTypeAttributeProvider(type);