Esempio n. 1
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 });
            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();
        }
Esempio n. 2
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();
		}
Esempio n. 3
0
        private void ExtractBAML(string name, Stream stream)
        {
            var reader = new Baml2006Reader(stream);
            var settings = new XamlObjectWriterSettings ();

            settings.BeforePropertiesHandler += (sender, e) =>
            {
                Trace("properties for {0}", e.Instance);
            };

            settings.XamlSetValueHandler += (sender, e) =>
            {
                //TraceTarget.Trace("set value {0} = {1}", e.Member.Name, e.Value);

                /*if (e.Member.Name == "DeferrableContent")
                {
                    e.Handled = true;
                }*/
            };

            var writer = new XamlObjectWriter(reader.SchemaContext, settings);
            while (reader.Read())
            {
                writer.WriteNode(reader);
            }
            var x = writer.Result;

            var ms = new MemoryStream();
            using (var xwriter = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true }))
            {
                System.Windows.Markup.XamlWriter.Save(x, xwriter);
            }

            var xamlname = Path.ChangeExtension(name, ".xaml");

            ms.Position = 0;
            AddResource(xamlname, ms);
        }