コード例 #1
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;
            }

            //Call TrySetProperty first and then TrySetValue to keep the code logic consistent whether it is through xaml or code.
            //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))
            {
                return;
            }

            //If it's a BindableProberty, SetValue
            if (xpe == null && TrySetValue(xamlelement, property, attached, value, lineInfo, serviceProvider, out xpe))
            {
                return;
            }

            //If it's an already initialized property, add to it
            if (xpe == null && TryAddToProperty(xamlelement, propertyName, value, xKey, lineInfo, serviceProvider, context, out xpe))
            {
                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;
            }
        }
コード例 #2
0
        public static object GetPropertyValue(object xamlElement, XmlName propertyName, HydrationContext context, IXmlLineInfo lineInfo, out object targetProperty)
        {
            var       localName = propertyName.LocalName;
            Exception xpe       = null;
            object    value;

            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 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);
            if (context.ExceptionHandler != null)
            {
                context.ExceptionHandler(xpe);
            }
            else
            {
                throw xpe;
            }

            return(null);
        }