Beispiel #1
0
        public void AttachedPropertyTest()
        {
            var generated   = new StringBuilder();
            var xmlSettings = new XmlWriterSettings {
                Indent = true, OmitXmlDeclaration = true
            };
            var xamlSettings = new XamlXmlWriterSettings {
                CloseOutput = true
            };
            var xsc = new XamlSchemaContext();

            using (System.Xaml.XamlXmlWriter writer = new System.Xaml.XamlXmlWriter(XmlWriter.Create(new StringWriter(generated), xmlSettings), xsc, xamlSettings))
            {
                XamlType   mc                     = xsc.GetXamlType(typeof(MediumContainer));
                XamlMember thing                  = mc.GetAttachableMember("Thing");
                var        developerType          = xsc.GetXamlType(typeof(Developer));
                var        developerExtensionType = xsc.GetXamlType(typeof(DeveloperExtension));
                string     ns                     = "clr-namespace:Test.Elements.AnotherNamespace;assembly=XamlTestClasses";
                writer.WriteNamespace(new NamespaceDeclaration(ns, "PreferredPrefix"));
                writer.WriteStartObject(developerType);
                writer.WriteStartMember(developerType.GetMember("Name"));
                writer.WriteValue("Andrew");
                writer.WriteEndMember();

                writer.WriteStartMember(developerType.GetMember("Friend"));
                writer.WriteStartObject(developerExtensionType);
                writer.WriteStartMember(developerExtensionType.GetMember("Name"));
                writer.WriteValue("");
                writer.WriteEndMember();
                writer.WriteEndObject();
                writer.WriteEndMember();

                writer.WriteStartMember(thing);
                writer.WriteValue("attached");
                writer.WriteEndMember();

                writer.WriteEndObject();
            }

            var target = @"<Developer Name=""Andrew"" Friend=""{Developer Name=&quot;&quot;}"" PreferredPrefix:MediumContainer.Thing=""attached"" xmlns=""clr-namespace:Test.Elements;assembly=XamlTestClasses"" xmlns:PreferredPrefix=""clr-namespace:Test.Elements.AnotherNamespace;assembly=XamlTestClasses"" />";

            Assert.AreEqual(target, generated.ToString());
        }
Beispiel #2
0
        public void EmptyStringTest()
        {
            var generated   = new StringBuilder();
            var xmlSettings = new XmlWriterSettings {
                Indent = true, OmitXmlDeclaration = true
            };
            var xamlSettings = new XamlXmlWriterSettings {
                CloseOutput = true
            };
            var xsc = new XamlSchemaContext();

            using (System.Xaml.XamlXmlWriter writer = new System.Xaml.XamlXmlWriter(XmlWriter.Create(new StringWriter(generated), xmlSettings), xsc, xamlSettings))
            {
                var developerType          = xsc.GetXamlType(typeof(Developer));
                var developerExtensionType = xsc.GetXamlType(typeof(DeveloperExtension));
                writer.WriteNamespace(new NamespaceDeclaration(XamlLanguage.Xaml2006Namespace, "x"));
                writer.WriteStartObject(developerType);
                writer.WriteStartMember(developerType.GetMember("Name"));
                writer.WriteValue("Andrew");
                writer.WriteEndMember();

                writer.WriteStartMember(developerType.GetMember("Friend"));
                writer.WriteStartObject(developerExtensionType);
                writer.WriteStartMember(developerExtensionType.GetMember("Name"));
                writer.WriteValue("");
                writer.WriteEndMember();
                writer.WriteEndObject();
                writer.WriteEndMember();

                writer.WriteEndObject();
            }

            var target = @"<Developer Name=""Andrew"" Friend=""{Developer Name=&quot;&quot;}"" xmlns=""clr-namespace:Test.Elements;assembly=XamlTestClasses"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" />";

            Assert.AreEqual(target, generated.ToString());
        }
        public string DecompileBaml(MemoryStream bamlCode, string containingAssemblyFile)
        {
            bamlCode.Position = 0;
            TextWriter w = new StringWriter();

            Assembly assembly = Assembly.LoadFile(containingAssemblyFile);

            Baml2006Reader reader = new Baml2006Reader(bamlCode, new XamlReaderSettings() { ValuesMustBeString = true, LocalAssembly = assembly });
            XamlXmlWriter writer = new XamlXmlWriter(new XmlTextWriter(w) { Formatting = Formatting.Indented }, reader.SchemaContext);
            while (reader.Read()) {
                switch (reader.NodeType) {
                    case XamlNodeType.None:

                        break;
                    case XamlNodeType.StartObject:
                        writer.WriteStartObject(reader.Type);
                        break;
                    case XamlNodeType.GetObject:
                        writer.WriteGetObject();
                        break;
                    case XamlNodeType.EndObject:
                        writer.WriteEndObject();
                        break;
                    case XamlNodeType.StartMember:
                        writer.WriteStartMember(reader.Member);
                        break;
                    case XamlNodeType.EndMember:
                        writer.WriteEndMember();
                        break;
                    case XamlNodeType.Value:
                        // requires XamlReaderSettings.ValuesMustBeString = true to work properly
                        writer.WriteValue(reader.Value);
                        break;
                    case XamlNodeType.NamespaceDeclaration:
                        writer.WriteNamespace(reader.Namespace);
                        break;
                    default:
                        throw new Exception("Invalid value for XamlNodeType");
                }
            }
            return w.ToString();
        }
Beispiel #4
0
		public string DecompileBaml(MemoryStream bamlCode, string containingAssemblyFile)
		{
			bamlCode.Position = 0;
			TextWriter w = new StringWriter();
			
			Assembly assembly = Assembly.LoadFile(containingAssemblyFile);
			
			Baml2006Reader reader = new Baml2006Reader(bamlCode, new XamlReaderSettings() { ValuesMustBeString = true, LocalAssembly = assembly });
			XDocument doc = new XDocument();
			XamlXmlWriter writer = new XamlXmlWriter(doc.CreateWriter(), reader.SchemaContext);
			while (reader.Read()) {
				switch (reader.NodeType) {
					case XamlNodeType.None:

						break;
					case XamlNodeType.StartObject:
						writer.WriteStartObject(reader.Type);
						break;
					case XamlNodeType.GetObject:
						writer.WriteGetObject();
						break;
					case XamlNodeType.EndObject:
						writer.WriteEndObject();
						break;
					case XamlNodeType.StartMember:
						writer.WriteStartMember(reader.Member);
						break;
					case XamlNodeType.EndMember:
						writer.WriteEndMember();
						break;
					case XamlNodeType.Value:
						// requires XamlReaderSettings.ValuesMustBeString = true to work properly
						writer.WriteValue(reader.Value);
						break;
					case XamlNodeType.NamespaceDeclaration:
						writer.WriteNamespace(reader.Namespace);
						break;
					default:
						throw new Exception("Invalid value for XamlNodeType");
				}
			}
			writer.Close();
			
			// Fix namespace references
			string suffixToRemove = ";assembly=" + assembly.GetName().Name;
			foreach (XAttribute attrib in doc.Root.Attributes()) {
				if (attrib.Name.Namespace == XNamespace.Xmlns) {
					if (attrib.Value.EndsWith(suffixToRemove, StringComparison.Ordinal)) {
						string newNamespace = attrib.Value.Substring(0, attrib.Value.Length - suffixToRemove.Length);
						ChangeXmlNamespace(doc, attrib.Value, newNamespace);
						attrib.Value = newNamespace;
					}
				}
			}
			// Convert x:Key into an attribute where possible
			XName xKey = XName.Get("Key", "http://schemas.microsoft.com/winfx/2006/xaml");
			foreach (XElement e in doc.Descendants(xKey).ToList()) {
				if (e.Nodes().Count() != 1)
					continue;
				XText text = e.Nodes().Single() as XText;
				if (text != null) {
					e.Parent.SetAttributeValue(xKey, text.Value);
					e.Remove();
				}
			}
			
			return doc.ToString();
		}