Пример #1
0
        static bool TrySetProperty(object element, string localName, object value, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, HydrationContext context, out Exception exception)
        {
            exception = null;

            var        elementType  = element.GetType();
            var        propertyInfo = elementType.GetRuntimeProperties().FirstOrDefault(p => p.Name == localName);
            MethodInfo setter;

            if (propertyInfo == null || !propertyInfo.CanWrite || (setter = propertyInfo.SetMethod) == null)
            {
                return(false);
            }

            if (!IsVisibleFrom(setter, context.RootElement))
            {
                return(false);
            }

            if (serviceProvider != null && serviceProvider.IProvideValueTarget != null)
            {
                ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = propertyInfo;
            }

            object convertedValue = value.ConvertTo(propertyInfo.PropertyType, () => propertyInfo, serviceProvider, out exception);

            if (exception != null || (convertedValue != null && !propertyInfo.PropertyType.IsInstanceOfType(convertedValue)))
            {
                return(false);
            }

            try {
                setter.Invoke(element, new object[] { convertedValue });
                return(true);
            }
            catch (Exception e) {
                exception = e;
                return(false);
            }
        }
Пример #2
0
        public static void SetPropertyValue(object xamlelement, XmlName propertyName, object value, object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo)
        {
            var       localName       = propertyName.LocalName;
            var       serviceProvider = new XamlServiceProvider(node, context);
            Exception xpe             = null;
            var       xKey            = node is IElementNode && ((IElementNode)node).Properties.ContainsKey(XmlName.xKey) ? ((ValueNode)((IElementNode)node).Properties[XmlName.xKey]).Value as string : null;

            //If it's an attached BP, update elementType and propertyName
            var bpOwnerType = xamlelement.GetType();
            var attached    = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo);
            var property    = GetBindableProperty(bpOwnerType, localName, lineInfo, false);

            //If the target is an event, connect
            if (xpe == null && TryConnectEvent(xamlelement, localName, attached, value, rootElement, lineInfo, out xpe))
            {
                return;
            }

            //If Value is DynamicResource and it's a BP, SetDynamicResource
            if (xpe == null && TrySetDynamicResource(xamlelement, property, value, lineInfo, out xpe))
            {
                return;
            }

            //If value is BindingBase, SetBinding
            if (xpe == null && TrySetBinding(xamlelement, property, localName, value, lineInfo, out xpe))
            {
                return;
            }

            var assemblyName = (context.RootAssembly ?? rootElement.GetType().GetTypeInfo().Assembly)?.GetName().Name;

            //If it's a BindableProberty, SetValue
            if (xpe == null && TrySetValue(xamlelement, property, attached, value, lineInfo, serviceProvider, out xpe))
            {
                if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType&& XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path)
                {
                    VisualDiagnostics.RegisterSourceInfo(value, new Uri($"{path};assembly={assemblyName}", UriKind.Relative), ((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition);
                }
                return;
            }

            //If we can assign that value to a normal property, let's do it
            if (xpe == null && TrySetProperty(xamlelement, localName, value, lineInfo, serviceProvider, context, out xpe))
            {
                if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType&& XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path)
                {
                    VisualDiagnostics.RegisterSourceInfo(value, new Uri($"{path};assembly={assemblyName}", UriKind.Relative), ((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition);
                }
                return;
            }

            //If it's an already initialized property, add to it
            if (xpe == null && TryAddToProperty(xamlelement, propertyName, value, xKey, lineInfo, serviceProvider, context, out xpe))
            {
                if (!(node is ValueNode) && value != null && !value.GetType().GetTypeInfo().IsValueType&& XamlFilePathAttribute.GetFilePathForObject(context.RootElement) is string path)
                {
                    VisualDiagnostics.RegisterSourceInfo(value, new Uri($"{path};assembly={assemblyName}", UriKind.Relative), ((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition);
                }
                return;
            }

            xpe = xpe ?? new XamlParseException($"Cannot assign property \"{localName}\": Property does not exist, or is not assignable, or mismatching type between value and property", lineInfo);
            if (context.ExceptionHandler != null)
            {
                context.ExceptionHandler(xpe);
            }
            else
            {
                throw xpe;
            }
        }
Пример #3
0
        public static object GetPropertyValue(object xamlElement, XmlName propertyName, HydrationContext context, IXmlLineInfo lineInfo, out Exception xpe, out object targetProperty)
        {
            var localName = propertyName.LocalName;

            xpe            = null;
            targetProperty = null;

            //If it's an attached BP, update elementType and propertyName
            var bpOwnerType = xamlElement.GetType();
            var attached    = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo);
            var property    = GetBindableProperty(bpOwnerType, localName, lineInfo, false);

            //If it's a BindableProberty, GetValue
            if (xpe == null && TryGetValue(xamlElement, property, attached, out var value, lineInfo, out xpe, out targetProperty))
            {
                return(value);
            }

            //If it's a normal property, get it
            if (xpe == null && TryGetProperty(xamlElement, localName, out value, lineInfo, context, out xpe, out targetProperty))
            {
                return(value);
            }

            xpe = xpe ?? new XamlParseException($"Property {localName} is not found or does not have an accessible getter", lineInfo);

            return(null);
        }
Пример #4
0
 public ApplyPropertiesVisitor(HydrationContext context, bool stopOnResourceDictionary = false)
 {
     Context = context;
     StopOnResourceDictionary = stopOnResourceDictionary;
 }
Пример #5
0
 public ExpandMarkupsVisitor(HydrationContext context) => Context = context;
Пример #6
0
 public NamescopingVisitor(HydrationContext context)
 {
 }
Пример #7
0
 public CreateValuesVisitor(HydrationContext context)
 {
     Context = context;
 }
 public FillResourceDictionariesVisitor(HydrationContext context)
 {
     Context = context;
 }