5
5
using System . Globalization ;
6
6
using System . Linq ;
7
7
using System . Reflection ;
8
- using System . Text ;
9
8
using System . Text . RegularExpressions ;
10
9
using Uno . Extensions ;
11
10
using Uno . UI ;
15
14
using Windows . UI . Xaml . Controls ;
16
15
using Windows . UI . Xaml . Data ;
17
16
using Windows . UI . Xaml . Documents ;
18
- using Windows . UI ;
19
- using Windows . Foundation ;
20
17
using Windows . UI . Text ;
18
+ using Windows . Foundation . Metadata ;
21
19
22
20
#if XAMARIN_ANDROID
23
21
using _View = Android . Views . View ;
@@ -38,7 +36,7 @@ internal class XamlObjectBuilder
38
36
private Queue < Action > _postActions = new Queue < Action > ( ) ;
39
37
private static readonly Regex _attachedPropertMatch = new Regex ( @"(\(.*?\))" ) ;
40
38
41
- private static Type [ ] _genericConvertibles = new [ ]
39
+ private static Type [ ] _genericConvertibles = new [ ]
42
40
{
43
41
typeof ( Media . Brush ) ,
44
42
typeof ( Media . SolidColorBrush ) ,
@@ -1006,6 +1004,39 @@ private void AddCollectionItems(object collectionInstance, IEnumerable<XamlObjec
1006
1004
1007
1005
private object ? BuildLiteralValue ( Type propertyType , string ? memberValue )
1008
1006
{
1007
+ if ( propertyType . GetCustomAttribute < CreateFromStringAttribute > ( ) is { } createFromString )
1008
+ {
1009
+ var sourceType = propertyType ;
1010
+ var methodName = createFromString . MethodName ;
1011
+ if ( createFromString . MethodName . Contains ( "." ) )
1012
+ {
1013
+ var splitIndex = createFromString . MethodName . LastIndexOf ( "." ) ;
1014
+ var typeName = createFromString . MethodName . Substring ( 0 , splitIndex ) ;
1015
+ sourceType = AppDomain . CurrentDomain
1016
+ . GetAssemblies ( )
1017
+ . Select ( a => a . GetType ( typeName ) )
1018
+ . Trim ( )
1019
+ . FirstOrDefault ( ) ;
1020
+ methodName = createFromString . MethodName . Substring ( splitIndex + 1 ) ;
1021
+ }
1022
+
1023
+ if ( sourceType ? . GetMethod ( methodName ) is { } conversionMethod && conversionMethod . IsStatic && ! conversionMethod . IsPrivate )
1024
+ {
1025
+ try
1026
+ {
1027
+ return conversionMethod . Invoke ( null , new object [ ] { memberValue } ) ;
1028
+ }
1029
+ catch ( Exception ex )
1030
+ {
1031
+ throw new XamlParseException ( "Executing [CreateFromString] method for type " + propertyType + " failed." , ex ) ;
1032
+ }
1033
+ }
1034
+ else
1035
+ {
1036
+ throw new XamlParseException ( "Method referenced by [CreateFromString] cannot be found for " + propertyType ) ;
1037
+ }
1038
+ }
1039
+
1009
1040
return Uno . UI . DataBinding . BindingPropertyHelper . Convert ( ( ) => propertyType , memberValue ) ;
1010
1041
}
1011
1042
0 commit comments