private static void WriteValue(Xaml.XamlReader xamlReader, XamlObjectWriter xamlWriter, XamlContextStack <WpfXamlFrame> stack, IStyleConnector styleConnector)
        {
            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlLanguage.Shared)
            {
                bool isShared;
                if (bool.TryParse(xamlReader.Value as string, out isShared))
                {
                    if (!isShared)
                    {
                        if (!(xamlReader is Baml2006Reader))
                        {
                            throw new XamlParseException(SR.Get(SRID.SharedAttributeInLooseXaml));
                        }
                    }
                }
            }
            // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown
            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlReaderHelper.Freeze)
            {
                bool freeze = Convert.ToBoolean(xamlReader.Value, TypeConverterHelper.InvariantEnglishUS);
                stack.CurrentFrame.FreezeFreezable = freeze;
                var bamlReader = xamlReader as System.Windows.Baml2006.Baml2006Reader;
                if (bamlReader != null)
                {
                    bamlReader.FreezeFreezables = freeze;
                }
            }
            // The space directive node stream should not be written because it induces object instantiation,
            // and the Baml2006Reader can produce space directives prematurely.
            else if (stack.CurrentFrame.Property == XmlSpace.Value || stack.CurrentFrame.Property == XamlLanguage.Space)
            {
                if (typeof(DependencyObject).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                {
                    System.Diagnostics.Debug.Assert(xamlReader.Value is string, "XmlAttributeProperties.XmlSpaceProperty has the type string.");
                    stack.CurrentFrame.XmlSpace = (string)xamlReader.Value == "default";
                }
            }
            else
            {
                // Ideally we should check if we're inside FrameworkTemplate's Content and not register those.
                // However, checking if the instance is null accomplishes the same with a much smaller perf impact.
                if (styleConnector != null &&
                    stack.CurrentFrame.Instance != null &&
                    stack.CurrentFrame.Property == XamlLanguage.ConnectionId &&
                    typeof(Style).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                {
                    styleConnector.Connect((int)xamlReader.Value, stack.CurrentFrame.Instance);
                }

                xamlWriter.WriteNode(xamlReader);
            }
        }
        private static object Load(System.Xaml.XamlReader xamlReader, IXamlObjectWriterFactory writerFactory,
                                   bool skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
        {
            XamlObjectWriter xamlWriter           = null;
            XamlContextStack <WpfXamlFrame> stack = new XamlContextStack <WpfXamlFrame>(() => new WpfXamlFrame());
            int persistId = 1;

            settings.AfterBeginInitHandler = delegate(object sender, System.Xaml.XamlObjectEventArgs args)
            {
                if (EventTrace.IsEnabled(EventTrace.Keyword.KeywordXamlBaml | EventTrace.Keyword.KeywordPerf, EventTrace.Level.Verbose))
                {
                    IXamlLineInfo ixli = xamlReader as IXamlLineInfo;

                    int lineNumber   = -1;
                    int linePosition = -1;

                    if (ixli != null && ixli.HasLineInfo)
                    {
                        lineNumber   = ixli.LineNumber;
                        linePosition = ixli.LinePosition;
                    }

                    EventTrace.EventProvider.TraceEvent(
                        EventTrace.Event.WClientParseXamlBamlInfo,
                        EventTrace.Keyword.KeywordXamlBaml | EventTrace.Keyword.KeywordPerf, EventTrace.Level.Verbose,
                        args.Instance == null ? 0 : PerfService.GetPerfElementID(args.Instance),
                        lineNumber,
                        linePosition);
                }

                UIElement uiElement = args.Instance as UIElement;
                if (uiElement != null)
                {
                    uiElement.SetPersistId(persistId++);
                }

                XamlSourceInfoHelper.SetXamlSourceInfo(args.Instance, args, baseUri);

                DependencyObject dObject = args.Instance as DependencyObject;
                if (dObject != null && stack.CurrentFrame.XmlnsDictionary != null)
                {
                    XmlnsDictionary dictionary = stack.CurrentFrame.XmlnsDictionary;
                    dictionary.Seal();

                    XmlAttributeProperties.SetXmlnsDictionary(dObject, dictionary);
                }

                stack.CurrentFrame.Instance = args.Instance;
            };
            if (writerFactory != null)
            {
                xamlWriter = writerFactory.GetXamlObjectWriter(settings);
            }
            else
            {
                xamlWriter = new System.Xaml.XamlObjectWriter(xamlReader.SchemaContext, settings);
            }

            IXamlLineInfo xamlLineInfo = null;

            try
            {
                //Handle Line Numbers
                xamlLineInfo = xamlReader as IXamlLineInfo;
                IXamlLineInfoConsumer xamlLineInfoConsumer = xamlWriter as IXamlLineInfoConsumer;
                bool shouldPassLineNumberInfo = false;
                if ((xamlLineInfo != null && xamlLineInfo.HasLineInfo) &&
                    (xamlLineInfoConsumer != null && xamlLineInfoConsumer.ShouldProvideLineInfo))
                {
                    shouldPassLineNumberInfo = true;
                }

                IStyleConnector styleConnector = rootObject as IStyleConnector;
                TransformNodes(xamlReader, xamlWriter,
                               false /*onlyLoadOneNode*/,
                               skipJournaledProperties,
                               shouldPassLineNumberInfo, xamlLineInfo, xamlLineInfoConsumer,
                               stack, styleConnector);
                xamlWriter.Close();
                return(xamlWriter.Result);
            }
            catch (Exception e)
            {
                // Don't wrap critical exceptions or already-wrapped exceptions.
                if (MS.Internal.CriticalExceptions.IsCriticalException(e) || !XamlReader.ShouldReWrapException(e, baseUri))
                {
                    throw;
                }
                XamlReader.RewrapException(e, xamlLineInfo, baseUri);
                return(null);    // this should never be executed
            }
        }
示例#3
0
        // The ordering of how things are created and which methods are called are ABSOLUTELY critical
        // Getting this order slightly off will result in several issues that are very hard to debug.
        //
        // The order must be:
        //      Register name to the TemplateNameScope (Either the real name specified by x:Name or
        //              RuntimeNameProperty or a fake name that we have to generate to call
        //              RegisterName.  This is CRUCIAL since RegisterName sets the TemplatedParent
        //              and the TemplateChildIndex on the object
        //      If we're dealing with the root, wire the object to the parent (via FE.TemplateChild
        //          if we're dealing with an FE as the container or using FEF if it's not an FE)
        //      Invalidate properties on the object
        private DependencyObject LoadOptimizedTemplateContent(DependencyObject container,
            IComponentConnector componentConnector,
            IStyleConnector styleConnector, List<DependencyObject> affectedChildren, UncommonField<Hashtable> templatedNonFeChildrenField)
        {
            if (Names == null)
            {
                Names = new XamlContextStack<Frame>(() => new Frame());
            }
            DependencyObject rootObject = null;

            if (TraceMarkup.IsEnabled)
            {
                TraceMarkup.Trace(TraceEventType.Start, TraceMarkup.Load);
            }

            FrameworkElement feContainer = container as FrameworkElement;
            bool isTemplatedParentAnFE = feContainer != null;

            TemplateNameScope nameScope = new TemplateNameScope(container, affectedChildren, this);
            XamlObjectWriterSettings settings = System.Windows.Markup.XamlReader.CreateObjectWriterSettings(_templateHolder.ObjectWriterParentSettings);
            settings.ExternalNameScope = nameScope;
            settings.RegisterNamesOnExternalNamescope = true;

            IEnumerator<String> nameEnumerator = ChildNames.GetEnumerator();

            // Delegate for AfterBeginInit event
            settings.AfterBeginInitHandler =
                delegate(object sender, System.Xaml.XamlObjectEventArgs args)
                {
                    HandleAfterBeginInit(args.Instance, ref rootObject, container, feContainer, nameScope, nameEnumerator);
                    if (XamlSourceInfoHelper.IsXamlSourceInfoEnabled)
                    {
                        XamlSourceInfoHelper.SetXamlSourceInfo(args.Instance, args, null);
                    }
                };
            // Delegate for BeforeProperties event
            settings.BeforePropertiesHandler =
                delegate(object sender, System.Xaml.XamlObjectEventArgs args)
                {
                    HandleBeforeProperties(args.Instance, ref rootObject, container, feContainer, nameScope);
                };
            // Delegate for XamlSetValue event
            settings.XamlSetValueHandler =
                delegate(object sender, System.Windows.Markup.XamlSetValueEventArgs setArgs)
            {
                setArgs.Handled = ReceivePropertySet(sender, setArgs.Member, setArgs.Value, container);
            };

            XamlObjectWriter objectWriter = _templateHolder.ObjectWriterFactory.GetXamlObjectWriter(settings);

            try
            {
                LoadTemplateXaml(objectWriter);
            }
            finally
            {
                if (TraceMarkup.IsEnabled)
                {
                    TraceMarkup.Trace(TraceEventType.Stop, TraceMarkup.Load, rootObject);
                }
            }
            return rootObject;
        }
        internal static void TransformNodes(System.Xaml.XamlReader xamlReader, System.Xaml.XamlObjectWriter xamlWriter,
                                            bool onlyLoadOneNode,
                                            bool skipJournaledProperties,
                                            bool shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer,
                                            XamlContextStack <WpfXamlFrame> stack,
                                            IStyleConnector styleConnector)
        {
            while (xamlReader.Read())
            {
                if (shouldPassLineNumberInfo)
                {
                    if (xamlLineInfo.LineNumber != 0)
                    {
                        xamlLineInfoConsumer.SetLineInfo(xamlLineInfo.LineNumber, xamlLineInfo.LinePosition);
                    }
                }

                switch (xamlReader.NodeType)
                {
                case System.Xaml.XamlNodeType.NamespaceDeclaration:
                    xamlWriter.WriteNode(xamlReader);
                    if (stack.Depth == 0 || stack.CurrentFrame.Type != null)
                    {
                        stack.PushScope();
                        // Need to create an XmlnsDictionary.
                        // Look up stack to see if we have one earlier
                        //  If so, use that.  Otherwise new a xmlnsDictionary
                        WpfXamlFrame iteratorFrame = stack.CurrentFrame;
                        while (iteratorFrame != null)
                        {
                            if (iteratorFrame.XmlnsDictionary != null)
                            {
                                stack.CurrentFrame.XmlnsDictionary =
                                    new XmlnsDictionary(iteratorFrame.XmlnsDictionary);
                                break;
                            }
                            iteratorFrame = (WpfXamlFrame)iteratorFrame.Previous;
                        }
                        if (stack.CurrentFrame.XmlnsDictionary == null)
                        {
                            stack.CurrentFrame.XmlnsDictionary =
                                new XmlnsDictionary();
                        }
                    }
                    stack.CurrentFrame.XmlnsDictionary.Add(xamlReader.Namespace.Prefix, xamlReader.Namespace.Namespace);
                    break;

                case System.Xaml.XamlNodeType.StartObject:
                    WriteStartObject(xamlReader, xamlWriter, stack);
                    break;

                case System.Xaml.XamlNodeType.GetObject:
                    xamlWriter.WriteNode(xamlReader);
                    // If there wasn't a namespace node before this get object, need to pushScope.
                    if (stack.CurrentFrame.Type != null)
                    {
                        stack.PushScope();
                    }
                    stack.CurrentFrame.Type = stack.PreviousFrame.Property.Type;
                    break;

                case System.Xaml.XamlNodeType.EndObject:
                    xamlWriter.WriteNode(xamlReader);
                    // Freeze if required
                    if (stack.CurrentFrame.FreezeFreezable)
                    {
                        Freezable freezable = xamlWriter.Result as Freezable;
                        if (freezable != null && freezable.CanFreeze)
                        {
                            freezable.Freeze();
                        }
                    }
                    DependencyObject dependencyObject = xamlWriter.Result as DependencyObject;
                    if (dependencyObject != null && stack.CurrentFrame.XmlSpace.HasValue)
                    {
                        XmlAttributeProperties.SetXmlSpace(dependencyObject, stack.CurrentFrame.XmlSpace.Value ? "default" : "preserve");
                    }
                    stack.PopScope();
                    break;

                case System.Xaml.XamlNodeType.StartMember:
                    // ObjectWriter should NOT process PresentationOptions:Freeze directive since it is Unknown
                    // The space directive node stream should not be written because it induces object instantiation,
                    // and the Baml2006Reader can produce space directives prematurely.
                    if (!(xamlReader.Member.IsDirective && xamlReader.Member == XamlReaderHelper.Freeze) &&
                        xamlReader.Member != XmlSpace.Value &&
                        xamlReader.Member != XamlLanguage.Space)
                    {
                        xamlWriter.WriteNode(xamlReader);
                    }

                    stack.CurrentFrame.Property = xamlReader.Member;
                    if (skipJournaledProperties)
                    {
                        if (!stack.CurrentFrame.Property.IsDirective)
                        {
                            System.Windows.Baml2006.WpfXamlMember wpfMember = stack.CurrentFrame.Property as System.Windows.Baml2006.WpfXamlMember;
                            if (wpfMember != null)
                            {
                                DependencyProperty prop = wpfMember.DependencyProperty;

                                if (prop != null)
                                {
                                    FrameworkPropertyMetadata metadata = prop.GetMetadata(stack.CurrentFrame.Type.UnderlyingType) as FrameworkPropertyMetadata;
                                    if (metadata != null && metadata.Journal == true)
                                    {
                                        // Ignore the BAML for this member, unless it declares a value that wasn't journaled - namely a binding or a dynamic resource
                                        int count = 1;
                                        while (xamlReader.Read())
                                        {
                                            switch (xamlReader.NodeType)
                                            {
                                            case System.Xaml.XamlNodeType.StartMember:
                                                count++;
                                                break;

                                            case System.Xaml.XamlNodeType.StartObject:
                                                XamlType xamlType            = xamlReader.Type;
                                                XamlType bindingBaseType     = xamlType.SchemaContext.GetXamlType(typeof(BindingBase));
                                                XamlType dynamicResourceType = xamlType.SchemaContext.GetXamlType(typeof(DynamicResourceExtension));
                                                if (count == 1 && (xamlType.CanAssignTo(bindingBaseType) || xamlType.CanAssignTo(dynamicResourceType)))
                                                {
                                                    count = 0;
                                                    WriteStartObject(xamlReader, xamlWriter, stack);
                                                }
                                                break;

                                            case System.Xaml.XamlNodeType.EndMember:
                                                count--;
                                                if (count == 0)
                                                {
                                                    xamlWriter.WriteNode(xamlReader);
                                                    stack.CurrentFrame.Property = null;
                                                }
                                                break;

                                            case System.Xaml.XamlNodeType.Value:
                                                DynamicResourceExtension value = xamlReader.Value as DynamicResourceExtension;
                                                if (value != null)
                                                {
                                                    WriteValue(xamlReader, xamlWriter, stack, styleConnector);
                                                }
                                                break;
                                            }
                                            if (count == 0)
                                            {
                                                break;
                                            }
                                        }

                                        System.Diagnostics.Debug.Assert(count == 0, "Mismatch StartMember/EndMember");
                                    }
                                }
                            }
                        }
                    }
                    break;

                case System.Xaml.XamlNodeType.EndMember:
                    WpfXamlFrame currentFrame    = stack.CurrentFrame;
                    XamlMember   currentProperty = currentFrame.Property;
                    // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown
                    // The space directive node stream should not be written because it induces object instantiation,
                    // and the Baml2006Reader can produce space directives prematurely.
                    if (!(currentProperty.IsDirective && currentProperty == XamlReaderHelper.Freeze) &&
                        currentProperty != XmlSpace.Value &&
                        currentProperty != XamlLanguage.Space)
                    {
                        xamlWriter.WriteNode(xamlReader);
                    }
                    currentFrame.Property = null;
                    break;

                case System.Xaml.XamlNodeType.Value:
                    WriteValue(xamlReader, xamlWriter, stack, styleConnector);
                    break;

                default:
                    xamlWriter.WriteNode(xamlReader);
                    break;
                }

                //Only do this loop for one node if loadAsync
                if (onlyLoadOneNode)
                {
                    return;
                }
            }
        }
 internal TemplateComponentConnector( IComponentConnector componentConnector, IStyleConnector styleConnector )
 {
     _styleConnector = styleConnector;
     _componentConnector = componentConnector;
 }
示例#6
0
        internal static void TransformNodes(System.Xaml.XamlReader xamlReader, System.Xaml.XamlObjectWriter xamlWriter,
                                         bool onlyLoadOneNode, 
                                         bool skipJournaledProperties,
                                         bool shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, 
                                         XamlContextStack<WpfXamlFrame> stack, 
                                         IStyleConnector styleConnector)
        { 
            while (xamlReader.Read())
            {
                if (shouldPassLineNumberInfo)
                { 
                    if (xamlLineInfo.LineNumber != 0)
                    { 
                        xamlLineInfoConsumer.SetLineInfo(xamlLineInfo.LineNumber, xamlLineInfo.LinePosition); 
                    }
                } 

                switch (xamlReader.NodeType)
                {
                case System.Xaml.XamlNodeType.NamespaceDeclaration: 
                    xamlWriter.WriteNode(xamlReader);
                    if (stack.Depth == 0 || stack.CurrentFrame.Type != null) 
                    { 
                        stack.PushScope();
                        // Need to create an XmlnsDictionary. 
                        // Look up stack to see if we have one earlier
                        //  If so, use that.  Otherwise new a xmlnsDictionary
                        WpfXamlFrame iteratorFrame = stack.CurrentFrame;
                        while (iteratorFrame != null) 
                        {
                            if (iteratorFrame.XmlnsDictionary != null) 
                            { 
                                stack.CurrentFrame.XmlnsDictionary =
                                    new XmlnsDictionary(iteratorFrame.XmlnsDictionary); 
                                break;
                            }
                            iteratorFrame = (WpfXamlFrame)iteratorFrame.Previous;
                        } 
                        if (stack.CurrentFrame.XmlnsDictionary == null)
                        { 
                            stack.CurrentFrame.XmlnsDictionary = 
                                     new XmlnsDictionary();
                        } 
                    }
                    stack.CurrentFrame.XmlnsDictionary.Add(xamlReader.Namespace.Prefix, xamlReader.Namespace.Namespace);
                    break;
                case System.Xaml.XamlNodeType.StartObject: 
                    xamlWriter.WriteNode(xamlReader);
                    // If there's a frame but no Type, that means there 
                    // was a namespace. Just set the Type 
                    if (stack.Depth != 0 && stack.CurrentFrame.Type == null)
                    { 
                        stack.CurrentFrame.Type = xamlReader.Type;
                    }
                    else
                    { 
                        // Propagate the FreezeFreezable property from the current stack frame
                        stack.PushScope(); 
                        stack.CurrentFrame.Type = xamlReader.Type; 
                        if (stack.PreviousFrame.FreezeFreezable)
                        { 
                            stack.CurrentFrame.FreezeFreezable = true;
                        }
                    }
                    break; 
                case System.Xaml.XamlNodeType.GetObject:
                    xamlWriter.WriteNode(xamlReader); 
                    // If there wasn't a namespace node before this get object, need to pushScope. 
                    if (stack.CurrentFrame.Type != null)
                    { 
                        stack.PushScope();
                    }
                    stack.CurrentFrame.Type = stack.PreviousFrame.Property.Type;
                    break; 
                case System.Xaml.XamlNodeType.EndObject:
                    xamlWriter.WriteNode(xamlReader); 
                    // Freeze if required 
                    if (stack.CurrentFrame.FreezeFreezable)
                    { 
                        Freezable freezable = xamlWriter.Result as Freezable;
                        if (freezable != null && freezable.CanFreeze)
                        {
                            freezable.Freeze(); 
                        }
                    } 
                    DependencyObject dependencyObject = xamlWriter.Result as DependencyObject; 
                    if (dependencyObject != null && stack.CurrentFrame.XmlSpace.HasValue)
                    { 
                        XmlAttributeProperties.SetXmlSpace(dependencyObject, stack.CurrentFrame.XmlSpace.Value ? "default" : "preserve");
                    }
                    stack.PopScope();
                    break; 
                case System.Xaml.XamlNodeType.StartMember:
                    // ObjectWriter should NOT process PresentationOptions:Freeze directive since it is Unknown 
                    // The space directive node stream should not be written because it induces object instantiation, 
                    // and the Baml2006Reader can produce space directives prematurely.
                    if (!(xamlReader.Member.IsDirective && xamlReader.Member == XamlReaderHelper.Freeze) && 
                        xamlReader.Member != XmlSpace.Value &&
                        xamlReader.Member != XamlLanguage.Space)
                    {
                        xamlWriter.WriteNode(xamlReader); 
                    }
 
                    stack.CurrentFrame.Property = xamlReader.Member; 
                    if (skipJournaledProperties)
                    { 
                        if (!stack.CurrentFrame.Property.IsDirective)
                        {
                            System.Windows.Baml2006.WpfXamlMember wpfMember = stack.CurrentFrame.Property as System.Windows.Baml2006.WpfXamlMember;
                            if (wpfMember != null) 
                            {
                                DependencyProperty prop = wpfMember.DependencyProperty; 
 
                                if (prop != null)
                                { 
                                    FrameworkPropertyMetadata metadata = prop.GetMetadata(stack.CurrentFrame.Type.UnderlyingType) as FrameworkPropertyMetadata;
                                    if (metadata != null && metadata.Journal == true)
                                    {
                                        // 
                                        int count = 1;
                                        while (xamlReader.Read()) 
                                        { 
                                            switch (xamlReader.NodeType)
                                            { 
                                            case System.Xaml.XamlNodeType.StartMember:
                                                count++;
                                                break;
                                            case System.Xaml.XamlNodeType.EndMember: 
                                                count--;
                                                if (count == 0) 
                                                { 
                                                    xamlWriter.WriteNode(xamlReader);
                                                } 
                                                break;
                                            }
                                            if (count == 0)
                                                break; 
                                        }
                                        // shouldn't this have been a XamlReader.Skip()? 
                                        System.Diagnostics.Debug.Assert(count == 0, "Mismatch StartMember/EndMember"); 
                                    }
                                } 
                            }
                        }
                    }
                    break; 
                case System.Xaml.XamlNodeType.EndMember:
                    WpfXamlFrame currentFrame = stack.CurrentFrame; 
                    XamlMember currentProperty = currentFrame.Property; 
                    // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown
                    // The space directive node stream should not be written because it induces object instantiation, 
                    // and the Baml2006Reader can produce space directives prematurely.
                    if (!(currentProperty.IsDirective && currentProperty == XamlReaderHelper.Freeze) &&
                        currentProperty != XmlSpace.Value &&
                        currentProperty != XamlLanguage.Space) 
                    {
                        xamlWriter.WriteNode(xamlReader); 
                    } 
                    currentFrame.Property = null;
                    break; 
                case System.Xaml.XamlNodeType.Value:
                    if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlLanguage.Shared)
                    {
                        bool isShared; 
                        if (bool.TryParse(xamlReader.Value as string, out isShared))
                        { 
                            if (!isShared) 
                            {
                                if (!(xamlReader is Baml2006Reader)) 
                                {
                                    throw new XamlParseException(SR.Get(SRID.SharedAttributeInLooseXaml));
                                }
                            } 
                        }
                    } 
                    // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown 
                    if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlReaderHelper.Freeze)
                    { 
                        bool freeze = Convert.ToBoolean(xamlReader.Value, TypeConverterHelper.InvariantEnglishUS);
                        stack.CurrentFrame.FreezeFreezable = freeze;
                        var bamlReader = xamlReader as System.Windows.Baml2006.Baml2006Reader;
                        if (bamlReader != null) 
                        {
                            bamlReader.FreezeFreezables = freeze; 
                        } 
                    }
                    // The space directive node stream should not be written because it induces object instantiation, 
                    // and the Baml2006Reader can produce space directives prematurely.
                    else if (stack.CurrentFrame.Property == XmlSpace.Value || stack.CurrentFrame.Property == XamlLanguage.Space)
                    {
                        if (typeof(DependencyObject).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType)) 
                        {
                            System.Diagnostics.Debug.Assert(xamlReader.Value is string, "XmlAttributeProperties.XmlSpaceProperty has the type string."); 
                            stack.CurrentFrame.XmlSpace = (string)xamlReader.Value == "default"; 
                        }
                    } 
                    else
                    {
                        // Ideally we should check if we're inside FrameworkTemplate's Content and not register those.
                        // However, checking if the instance is null accomplishes the same with a much smaller perf impact. 
                        if (styleConnector != null &&
                            stack.CurrentFrame.Instance != null && 
                            stack.CurrentFrame.Property == XamlLanguage.ConnectionId && 
                            typeof(Style).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                        { 
                            styleConnector.Connect((int)xamlReader.Value, stack.CurrentFrame.Instance);
                        }

                        xamlWriter.WriteNode(xamlReader); 
                    }
                    break; 
                default: 
                    xamlWriter.WriteNode(xamlReader);
                    break; 
                }

                //Only do this loop for one node if loadAsync
                if (onlyLoadOneNode) 
                {
                    return; 
                } 
            }
        } 
示例#7
0
        internal static void TransformNodes(System.Xaml.XamlReader xamlReader, System.Xaml.XamlObjectWriter xamlWriter,
                                            bool onlyLoadOneNode,
                                            bool skipJournaledProperties,
                                            bool shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer,
                                            XamlContextStack <WpfXamlFrame> stack,
                                            IStyleConnector styleConnector)
        {
            while (xamlReader.Read())
            {
                if (shouldPassLineNumberInfo)
                {
                    if (xamlLineInfo.LineNumber != 0)
                    {
                        xamlLineInfoConsumer.SetLineInfo(xamlLineInfo.LineNumber, xamlLineInfo.LinePosition);
                    }
                }

                switch (xamlReader.NodeType)
                {
                case System.Xaml.XamlNodeType.NamespaceDeclaration:
                    xamlWriter.WriteNode(xamlReader);
                    if (stack.Depth == 0 || stack.CurrentFrame.Type != null)
                    {
                        stack.PushScope();
                        // Need to create an XmlnsDictionary.
                        // Look up stack to see if we have one earlier
                        //  If so, use that.  Otherwise new a xmlnsDictionary
                        WpfXamlFrame iteratorFrame = stack.CurrentFrame;
                        while (iteratorFrame != null)
                        {
                            if (iteratorFrame.XmlnsDictionary != null)
                            {
                                stack.CurrentFrame.XmlnsDictionary =
                                    new XmlnsDictionary(iteratorFrame.XmlnsDictionary);
                                break;
                            }
                            iteratorFrame = (WpfXamlFrame)iteratorFrame.Previous;
                        }
                        if (stack.CurrentFrame.XmlnsDictionary == null)
                        {
                            stack.CurrentFrame.XmlnsDictionary =
                                new XmlnsDictionary();
                        }
                    }
                    stack.CurrentFrame.XmlnsDictionary.Add(xamlReader.Namespace.Prefix, xamlReader.Namespace.Namespace);
                    break;

                case System.Xaml.XamlNodeType.StartObject:
                    xamlWriter.WriteNode(xamlReader);
                    // If there's a frame but no Type, that means there
                    // was a namespace. Just set the Type
                    if (stack.Depth != 0 && stack.CurrentFrame.Type == null)
                    {
                        stack.CurrentFrame.Type = xamlReader.Type;
                    }
                    else
                    {
                        // Propagate the FreezeFreezable property from the current stack frame
                        stack.PushScope();
                        stack.CurrentFrame.Type = xamlReader.Type;
                        if (stack.PreviousFrame.FreezeFreezable)
                        {
                            stack.CurrentFrame.FreezeFreezable = true;
                        }
                    }
                    break;

                case System.Xaml.XamlNodeType.GetObject:
                    xamlWriter.WriteNode(xamlReader);
                    // If there wasn't a namespace node before this get object, need to pushScope.
                    if (stack.CurrentFrame.Type != null)
                    {
                        stack.PushScope();
                    }
                    stack.CurrentFrame.Type = stack.PreviousFrame.Property.Type;
                    break;

                case System.Xaml.XamlNodeType.EndObject:
                    xamlWriter.WriteNode(xamlReader);
                    // Freeze if required
                    if (stack.CurrentFrame.FreezeFreezable)
                    {
                        Freezable freezable = xamlWriter.Result as Freezable;
                        if (freezable != null && freezable.CanFreeze)
                        {
                            freezable.Freeze();
                        }
                    }
                    DependencyObject dependencyObject = xamlWriter.Result as DependencyObject;
                    if (dependencyObject != null && stack.CurrentFrame.XmlSpace.HasValue)
                    {
                        XmlAttributeProperties.SetXmlSpace(dependencyObject, stack.CurrentFrame.XmlSpace.Value ? "default" : "preserve");
                    }
                    stack.PopScope();
                    break;

                case System.Xaml.XamlNodeType.StartMember:
                    // ObjectWriter should NOT process PresentationOptions:Freeze directive since it is Unknown
                    // The space directive node stream should not be written because it induces object instantiation,
                    // and the Baml2006Reader can produce space directives prematurely.
                    if (!(xamlReader.Member.IsDirective && xamlReader.Member == XamlReaderHelper.Freeze) &&
                        xamlReader.Member != XmlSpace.Value &&
                        xamlReader.Member != XamlLanguage.Space)
                    {
                        xamlWriter.WriteNode(xamlReader);
                    }

                    stack.CurrentFrame.Property = xamlReader.Member;
                    if (skipJournaledProperties)
                    {
                        if (!stack.CurrentFrame.Property.IsDirective)
                        {
                            System.Windows.Baml2006.WpfXamlMember wpfMember = stack.CurrentFrame.Property as System.Windows.Baml2006.WpfXamlMember;
                            if (wpfMember != null)
                            {
                                DependencyProperty prop = wpfMember.DependencyProperty;

                                if (prop != null)
                                {
                                    FrameworkPropertyMetadata metadata = prop.GetMetadata(stack.CurrentFrame.Type.UnderlyingType) as FrameworkPropertyMetadata;
                                    if (metadata != null && metadata.Journal == true)
                                    {
                                        //
                                        int count = 1;
                                        while (xamlReader.Read())
                                        {
                                            switch (xamlReader.NodeType)
                                            {
                                            case System.Xaml.XamlNodeType.StartMember:
                                                count++;
                                                break;

                                            case System.Xaml.XamlNodeType.EndMember:
                                                count--;
                                                if (count == 0)
                                                {
                                                    xamlWriter.WriteNode(xamlReader);
                                                }
                                                break;
                                            }
                                            if (count == 0)
                                            {
                                                break;
                                            }
                                        }
                                        // shouldn't this have been a XamlReader.Skip()?
                                        System.Diagnostics.Debug.Assert(count == 0, "Mismatch StartMember/EndMember");
                                    }
                                }
                            }
                        }
                    }
                    break;

                case System.Xaml.XamlNodeType.EndMember:
                    WpfXamlFrame currentFrame    = stack.CurrentFrame;
                    XamlMember   currentProperty = currentFrame.Property;
                    // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown
                    // The space directive node stream should not be written because it induces object instantiation,
                    // and the Baml2006Reader can produce space directives prematurely.
                    if (!(currentProperty.IsDirective && currentProperty == XamlReaderHelper.Freeze) &&
                        currentProperty != XmlSpace.Value &&
                        currentProperty != XamlLanguage.Space)
                    {
                        xamlWriter.WriteNode(xamlReader);
                    }
                    currentFrame.Property = null;
                    break;

                case System.Xaml.XamlNodeType.Value:
                    if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlLanguage.Shared)
                    {
                        bool isShared;
                        if (bool.TryParse(xamlReader.Value as string, out isShared))
                        {
                            if (!isShared)
                            {
                                if (!(xamlReader is Baml2006Reader))
                                {
                                    throw new XamlParseException(SR.Get(SRID.SharedAttributeInLooseXaml));
                                }
                            }
                        }
                    }
                    // ObjectWriter should not process PresentationOptions:Freeze directive nodes since it is unknown
                    if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlReaderHelper.Freeze)
                    {
                        bool freeze = Convert.ToBoolean(xamlReader.Value, TypeConverterHelper.InvariantEnglishUS);
                        stack.CurrentFrame.FreezeFreezable = freeze;
                        var bamlReader = xamlReader as System.Windows.Baml2006.Baml2006Reader;
                        if (bamlReader != null)
                        {
                            bamlReader.FreezeFreezables = freeze;
                        }
                    }
                    // The space directive node stream should not be written because it induces object instantiation,
                    // and the Baml2006Reader can produce space directives prematurely.
                    else if (stack.CurrentFrame.Property == XmlSpace.Value || stack.CurrentFrame.Property == XamlLanguage.Space)
                    {
                        if (typeof(DependencyObject).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                        {
                            System.Diagnostics.Debug.Assert(xamlReader.Value is string, "XmlAttributeProperties.XmlSpaceProperty has the type string.");
                            stack.CurrentFrame.XmlSpace = (string)xamlReader.Value == "default";
                        }
                    }
                    else
                    {
                        // Ideally we should check if we're inside FrameworkTemplate's Content and not register those.
                        // However, checking if the instance is null accomplishes the same with a much smaller perf impact.
                        if (styleConnector != null &&
                            stack.CurrentFrame.Instance != null &&
                            stack.CurrentFrame.Property == XamlLanguage.ConnectionId &&
                            typeof(Style).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                        {
                            styleConnector.Connect((int)xamlReader.Value, stack.CurrentFrame.Instance);
                        }

                        xamlWriter.WriteNode(xamlReader);
                    }
                    break;

                default:
                    xamlWriter.WriteNode(xamlReader);
                    break;
                }

                //Only do this loop for one node if loadAsync
                if (onlyLoadOneNode)
                {
                    return;
                }
            }
        }
 // Token: 0x06002256 RID: 8790 RVA: 0x000AAC86 File Offset: 0x000A8E86
 internal TemplateComponentConnector(IComponentConnector componentConnector, IStyleConnector styleConnector)
 {
     this._styleConnector     = styleConnector;
     this._componentConnector = componentConnector;
 }
示例#9
0
        // Token: 0x0600226A RID: 8810 RVA: 0x000AAE10 File Offset: 0x000A9010
        private static object Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, bool skipJournaledProperties, object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
        {
            XamlContextStack <WpfXamlFrame> stack = new XamlContextStack <WpfXamlFrame>(() => new WpfXamlFrame());
            int persistId = 1;

            settings.AfterBeginInitHandler = delegate(object sender, XamlObjectEventArgs args)
            {
                if (EventTrace.IsEnabled(EventTrace.Keyword.KeywordPerf | EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose))
                {
                    IXamlLineInfo xamlLineInfo2 = xamlReader as IXamlLineInfo;
                    int           num           = -1;
                    int           num2          = -1;
                    if (xamlLineInfo2 != null && xamlLineInfo2.HasLineInfo)
                    {
                        num  = xamlLineInfo2.LineNumber;
                        num2 = xamlLineInfo2.LinePosition;
                    }
                    EventTrace.EventProvider.TraceEvent(EventTrace.Event.WClientParseXamlBamlInfo, EventTrace.Keyword.KeywordPerf | EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose, new object[]
                    {
                        (args.Instance == null) ? 0L : PerfService.GetPerfElementID(args.Instance),
                        num,
                        num2
                    });
                }
                UIElement uielement = args.Instance as UIElement;
                if (uielement != null)
                {
                    UIElement uielement2 = uielement;
                    int       persistId  = persistId;
                    persistId++;
                    uielement2.SetPersistId(persistId);
                }
                XamlSourceInfoHelper.SetXamlSourceInfo(args.Instance, args, baseUri);
                DependencyObject dependencyObject = args.Instance as DependencyObject;
                if (dependencyObject != null && stack.CurrentFrame.XmlnsDictionary != null)
                {
                    XmlnsDictionary xmlnsDictionary = stack.CurrentFrame.XmlnsDictionary;
                    xmlnsDictionary.Seal();
                    XmlAttributeProperties.SetXmlnsDictionary(dependencyObject, xmlnsDictionary);
                }
                stack.CurrentFrame.Instance = args.Instance;
            };
            XamlObjectWriter xamlObjectWriter;

            if (writerFactory != null)
            {
                xamlObjectWriter = writerFactory.GetXamlObjectWriter(settings);
            }
            else
            {
                xamlObjectWriter = new XamlObjectWriter(xamlReader.SchemaContext, settings);
            }
            IXamlLineInfo xamlLineInfo = null;
            object        result;

            try
            {
                xamlLineInfo = (xamlReader as IXamlLineInfo);
                IXamlLineInfoConsumer xamlLineInfoConsumer = xamlObjectWriter;
                bool shouldPassLineNumberInfo = false;
                if (xamlLineInfo != null && xamlLineInfo.HasLineInfo && xamlLineInfoConsumer != null && xamlLineInfoConsumer.ShouldProvideLineInfo)
                {
                    shouldPassLineNumberInfo = true;
                }
                IStyleConnector styleConnector = rootObject as IStyleConnector;
                WpfXamlLoader.TransformNodes(xamlReader, xamlObjectWriter, false, skipJournaledProperties, shouldPassLineNumberInfo, xamlLineInfo, xamlLineInfoConsumer, stack, styleConnector);
                xamlObjectWriter.Close();
                result = xamlObjectWriter.Result;
            }
            catch (Exception ex)
            {
                if (CriticalExceptions.IsCriticalException(ex) || !XamlReader.ShouldReWrapException(ex, baseUri))
                {
                    throw;
                }
                XamlReader.RewrapException(ex, xamlLineInfo, baseUri);
                result = null;
            }
            return(result);
        }
示例#10
0
        // Token: 0x0600226C RID: 8812 RVA: 0x000AB378 File Offset: 0x000A9578
        private static void WriteValue(XamlReader xamlReader, XamlObjectWriter xamlWriter, XamlContextStack <WpfXamlFrame> stack, IStyleConnector styleConnector)
        {
            bool flag;

            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlLanguage.Shared && bool.TryParse(xamlReader.Value as string, out flag) && !flag && !(xamlReader is Baml2006Reader))
            {
                throw new XamlParseException(SR.Get("SharedAttributeInLooseXaml"));
            }
            if (stack.CurrentFrame.Property.IsDirective && stack.CurrentFrame.Property == XamlReaderHelper.Freeze)
            {
                bool flag2 = Convert.ToBoolean(xamlReader.Value, TypeConverterHelper.InvariantEnglishUS);
                stack.CurrentFrame.FreezeFreezable = flag2;
                Baml2006Reader baml2006Reader = xamlReader as Baml2006Reader;
                if (baml2006Reader != null)
                {
                    baml2006Reader.FreezeFreezables = flag2;
                    return;
                }
            }
            else if (stack.CurrentFrame.Property == WpfXamlLoader.XmlSpace.Value || stack.CurrentFrame.Property == XamlLanguage.Space)
            {
                if (typeof(DependencyObject).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                {
                    stack.CurrentFrame.XmlSpace = new bool?((string)xamlReader.Value == "default");
                    return;
                }
            }
            else
            {
                if (styleConnector != null && stack.CurrentFrame.Instance != null && stack.CurrentFrame.Property == XamlLanguage.ConnectionId && typeof(Style).IsAssignableFrom(stack.CurrentFrame.Type.UnderlyingType))
                {
                    styleConnector.Connect((int)xamlReader.Value, stack.CurrentFrame.Instance);
                }
                xamlWriter.WriteNode(xamlReader);
            }
        }
示例#11
0
        // Token: 0x0600226B RID: 8811 RVA: 0x000AAF38 File Offset: 0x000A9138
        internal static void TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, bool onlyLoadOneNode, bool skipJournaledProperties, bool shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack <WpfXamlFrame> stack, IStyleConnector styleConnector)
        {
            while (xamlReader.Read())
            {
                if (shouldPassLineNumberInfo && xamlLineInfo.LineNumber != 0)
                {
                    xamlLineInfoConsumer.SetLineInfo(xamlLineInfo.LineNumber, xamlLineInfo.LinePosition);
                }
                switch (xamlReader.NodeType)
                {
                case XamlNodeType.StartObject:
                    WpfXamlLoader.WriteStartObject(xamlReader, xamlWriter, stack);
                    break;

                case XamlNodeType.GetObject:
                    xamlWriter.WriteNode(xamlReader);
                    if (stack.CurrentFrame.Type != null)
                    {
                        stack.PushScope();
                    }
                    stack.CurrentFrame.Type = stack.PreviousFrame.Property.Type;
                    break;

                case XamlNodeType.EndObject:
                {
                    xamlWriter.WriteNode(xamlReader);
                    if (stack.CurrentFrame.FreezeFreezable)
                    {
                        Freezable freezable = xamlWriter.Result as Freezable;
                        if (freezable != null && freezable.CanFreeze)
                        {
                            freezable.Freeze();
                        }
                    }
                    DependencyObject dependencyObject = xamlWriter.Result as DependencyObject;
                    if (dependencyObject != null && stack.CurrentFrame.XmlSpace != null)
                    {
                        XmlAttributeProperties.SetXmlSpace(dependencyObject, stack.CurrentFrame.XmlSpace.Value ? "default" : "preserve");
                    }
                    stack.PopScope();
                    break;
                }

                case XamlNodeType.StartMember:
                    if ((!xamlReader.Member.IsDirective || !(xamlReader.Member == XamlReaderHelper.Freeze)) && xamlReader.Member != WpfXamlLoader.XmlSpace.Value && xamlReader.Member != XamlLanguage.Space)
                    {
                        xamlWriter.WriteNode(xamlReader);
                    }
                    stack.CurrentFrame.Property = xamlReader.Member;
                    if (skipJournaledProperties && !stack.CurrentFrame.Property.IsDirective)
                    {
                        WpfXamlMember wpfXamlMember = stack.CurrentFrame.Property as WpfXamlMember;
                        if (wpfXamlMember != null)
                        {
                            DependencyProperty dependencyProperty = wpfXamlMember.DependencyProperty;
                            if (dependencyProperty != null)
                            {
                                FrameworkPropertyMetadata frameworkPropertyMetadata = dependencyProperty.GetMetadata(stack.CurrentFrame.Type.UnderlyingType) as FrameworkPropertyMetadata;
                                if (frameworkPropertyMetadata != null && frameworkPropertyMetadata.Journal)
                                {
                                    int num = 1;
                                    while (xamlReader.Read())
                                    {
                                        switch (xamlReader.NodeType)
                                        {
                                        case XamlNodeType.StartObject:
                                        {
                                            XamlType type      = xamlReader.Type;
                                            XamlType xamlType  = type.SchemaContext.GetXamlType(typeof(BindingBase));
                                            XamlType xamlType2 = type.SchemaContext.GetXamlType(typeof(DynamicResourceExtension));
                                            if (num == 1 && (type.CanAssignTo(xamlType) || type.CanAssignTo(xamlType2)))
                                            {
                                                num = 0;
                                                WpfXamlLoader.WriteStartObject(xamlReader, xamlWriter, stack);
                                            }
                                            break;
                                        }

                                        case XamlNodeType.StartMember:
                                            num++;
                                            break;

                                        case XamlNodeType.EndMember:
                                            num--;
                                            if (num == 0)
                                            {
                                                xamlWriter.WriteNode(xamlReader);
                                                stack.CurrentFrame.Property = null;
                                            }
                                            break;

                                        case XamlNodeType.Value:
                                        {
                                            DynamicResourceExtension dynamicResourceExtension = xamlReader.Value as DynamicResourceExtension;
                                            if (dynamicResourceExtension != null)
                                            {
                                                WpfXamlLoader.WriteValue(xamlReader, xamlWriter, stack, styleConnector);
                                            }
                                            break;
                                        }
                                        }
                                        if (num == 0)
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;

                case XamlNodeType.EndMember:
                {
                    WpfXamlFrame currentFrame = stack.CurrentFrame;
                    XamlMember   property     = currentFrame.Property;
                    if ((!property.IsDirective || !(property == XamlReaderHelper.Freeze)) && property != WpfXamlLoader.XmlSpace.Value && property != XamlLanguage.Space)
                    {
                        xamlWriter.WriteNode(xamlReader);
                    }
                    currentFrame.Property = null;
                    break;
                }

                case XamlNodeType.Value:
                    WpfXamlLoader.WriteValue(xamlReader, xamlWriter, stack, styleConnector);
                    break;

                case XamlNodeType.NamespaceDeclaration:
                    xamlWriter.WriteNode(xamlReader);
                    if (stack.Depth == 0 || stack.CurrentFrame.Type != null)
                    {
                        stack.PushScope();
                        for (WpfXamlFrame wpfXamlFrame = stack.CurrentFrame; wpfXamlFrame != null; wpfXamlFrame = (WpfXamlFrame)wpfXamlFrame.Previous)
                        {
                            if (wpfXamlFrame.XmlnsDictionary != null)
                            {
                                stack.CurrentFrame.XmlnsDictionary = new XmlnsDictionary(wpfXamlFrame.XmlnsDictionary);
                                break;
                            }
                        }
                        if (stack.CurrentFrame.XmlnsDictionary == null)
                        {
                            stack.CurrentFrame.XmlnsDictionary = new XmlnsDictionary();
                        }
                    }
                    stack.CurrentFrame.XmlnsDictionary.Add(xamlReader.Namespace.Prefix, xamlReader.Namespace.Namespace);
                    break;

                default:
                    xamlWriter.WriteNode(xamlReader);
                    break;
                }
                if (onlyLoadOneNode)
                {
                    return;
                }
            }
        }