// Copy the root namespaces from a reader to a writer.
        // DesignTimeXamlWriter follows proper XAML convention by omitting the assembly name from
        // clr-namespaces in the local assembly. However, VB Expressions aren't local-assembly-aware,
        // and require an assembly name. So for every clr-namespace with no assembly name, we add an
        // additional namespace record with an assembly name, to support VB.
        // We only do this at the root level, since the designer only writes out namespaces at the root level.
        internal void CopyNamespacesAndAddLocalAssembly(System.Xaml.XamlReader activityBuilderReader, System.Xaml.XamlWriter objectWriter)
        {
            // Designer loads alwas provide line info
            IXamlLineInfo               lineInfo           = (IXamlLineInfo)activityBuilderReader;
            IXamlLineInfoConsumer       lineInfoConsumer   = (IXamlLineInfoConsumer)objectWriter;
            HashSet <string>            definedPrefixes    = new HashSet <string>();
            List <NamespaceDeclaration> localAsmNamespaces = null;

            while (activityBuilderReader.Read())
            {
                lineInfoConsumer.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);

                if (activityBuilderReader.NodeType == XamlNodeType.NamespaceDeclaration)
                {
                    definedPrefixes.Add(activityBuilderReader.Namespace.Prefix);
                    if (this.XamlSchemaContext.IsClrNamespaceWithNoAssembly(activityBuilderReader.Namespace.Namespace))
                    {
                        if (localAsmNamespaces == null)
                        {
                            localAsmNamespaces = new List <NamespaceDeclaration>();
                        }

                        localAsmNamespaces.Add(activityBuilderReader.Namespace);
                    }

                    objectWriter.WriteNode(activityBuilderReader);
                }
                else
                {
                    if (localAsmNamespaces != null)
                    {
                        foreach (NamespaceDeclaration ns in localAsmNamespaces)
                        {
                            string prefix = null;
                            int    i      = 0;
                            do
                            {
                                i++;
                                prefix = ns.Prefix + i.ToString(CultureInfo.InvariantCulture);
                            }while (definedPrefixes.Contains(prefix));
                            string fullNs = this.XamlSchemaContext.AddLocalAssembly(ns.Namespace);
                            objectWriter.WriteNamespace(new NamespaceDeclaration(fullNs, prefix));
                            definedPrefixes.Add(prefix);
                        }
                    }

                    objectWriter.WriteNode(activityBuilderReader);
                    return;
                }
            }
        }
示例#2
0
        public override void  WriteNamespace(NamespaceDeclaration namespaceDeclaration)
        {
            switch (_mode)
            {
            case DeferringMode.Off:
                return;

            case DeferringMode.TemplateReady:
                throw new XamlInternalException(SR.Get(SRID.TemplateNotCollected, "WriteNamespace"));

            case DeferringMode.TemplateStarting:
                StartDeferredList();
                _mode = DeferringMode.TemplateDeferring;
                goto case DeferringMode.TemplateDeferring;

            case DeferringMode.TemplateDeferring:
                _deferredWriter.WriteNamespace(namespaceDeclaration);
                _handled = true;
                break;

            default:
                throw new XamlInternalException(SR.Get(SRID.MissingCase, _mode.ToString(), "WriteNamespace"));
            }
        }
示例#3
0
			public override void WriteTo(XamlWriter writer)
			{
				Log("NamespaceDeclaration {0}", this.Namespace);
				Debug.Assert(this.Children.Count == 0);
				writer.WriteNamespace(this.Namespace);
			}
        void WritestrippedXamlNode(XamlReader reader, XamlWriter writer)
        {
            switch (reader.NodeType)
            {
                case XamlNodeType.StartObject:
                    XamlType xamlType = reader.Type;
                    if (xamlType.IsUnknown)
                    {
                        IList<XamlType> typeArgs = UpdateTypeArgs(xamlType.TypeArguments, reader.SchemaContext);
                        string xmlns = XamlBuildTaskServices.UpdateClrNamespaceUriWithLocalAssembly(xamlType.PreferredXamlNamespace, this.localAssemblyName);
                        xamlType = new XamlType(xmlns, xamlType.Name, typeArgs, reader.SchemaContext);
                    }
                    writer.WriteStartObject(xamlType);
                    break;

                case XamlNodeType.StartMember:
                    XamlMember member = reader.Member;
                    if (member.IsUnknown && !member.IsDirective)
                    {
                        string xmlns = XamlBuildTaskServices.UpdateClrNamespaceUriWithLocalAssembly(member.DeclaringType.PreferredXamlNamespace, this.localAssemblyName);
                        XamlType memberXamlType = new XamlType(xmlns, member.DeclaringType.Name, member.DeclaringType.TypeArguments, reader.SchemaContext);
                        member = new XamlMember(member.Name, memberXamlType, member.IsAttachable);
                    }
                    writer.WriteStartMember(member);
                    break;

                case XamlNodeType.NamespaceDeclaration:
                    NamespaceDeclaration ns = new NamespaceDeclaration(
                        XamlBuildTaskServices.UpdateClrNamespaceUriWithLocalAssembly(reader.Namespace.Namespace, this.localAssemblyName),
                        reader.Namespace.Prefix);
                    writer.WriteNamespace(ns);
                    break;

                case XamlNodeType.GetObject:
                case XamlNodeType.EndObject:
                case XamlNodeType.EndMember:
                case XamlNodeType.Value:
                case XamlNodeType.None:
                    writer.WriteNode(reader);
                    break;

                default:
                    Debug.Fail("Unrecognized XamlNodeType value" + reader.NodeType.ToString());
                    break;
            }
        }