Пример #1
0
        internal static object CreateObjectFromAttributeText(string valueText, XamlPropertyInfo targetProperty, XamlObject scope)
        {
            try
            {
                if (targetProperty.ReturnType == typeof(Uri))
                {
                    return(scope.OwnerDocument.TypeFinder.ConvertUriToLocalUri(new Uri(valueText, UriKind.RelativeOrAbsolute)));
                }
                else if (targetProperty.ReturnType == typeof(ImageSource))
                {
                    var uri =
                        scope.OwnerDocument.TypeFinder.ConvertUriToLocalUri(new Uri(valueText, UriKind.RelativeOrAbsolute));
                    return(targetProperty.TypeConverter.ConvertFromString(
                               scope.OwnerDocument.GetTypeDescriptorContext(scope), CultureInfo.InvariantCulture, uri.ToString()));
                }
                if (targetProperty.ReturnType == typeof(Brush))
                {
                    return(new BrushConverter().ConvertFromString(valueText) as SolidColorBrush);
                }

                return(targetProperty.TypeConverter.ConvertFromString(
                           scope.OwnerDocument.GetTypeDescriptorContext(scope),
                           CultureInfo.InvariantCulture, valueText));
            }
            catch (Exception e) {
                IXamlErrorSink sink = (IXamlErrorSink)scope.OwnerDocument.ServiceProvider.GetService(typeof(IXamlErrorSink));
                if (sink != null)
                {
                    sink.ReportError(e.Message + " in property: " + targetProperty.FullyQualifiedName, 0, 0);
                }
            }
            return(null);
        }
Пример #2
0
        internal static object CreateObjectFromAttributeText(string valueText, Type targetType, XamlObject scope)
        {
            try {
                var converter =
                    XamlNormalPropertyInfo.GetCustomTypeConverter(targetType) ??
                    TypeDescriptor.GetConverter(targetType);

                return(converter.ConvertFromInvariantString(
                           scope.OwnerDocument.GetTypeDescriptorContext(scope), valueText));
            }
            catch (Exception e) {
                IXamlErrorSink sink = (IXamlErrorSink)scope.OwnerDocument.ServiceProvider.GetService(typeof(IXamlErrorSink));
                if (sink != null)
                {
                    sink.ReportError(e.Message, 0, 0);
                }
            }
            return(null);
        }