How do can I read through a baml stream that contains a ResourceDictionaory using the Baml2006Reader and without acually instantiating the the ResourceDictionary?
I can ready through regular baml that just contains a UserControl just fine and I can examine the XAML tree using Baml2006Reader.NodeType etc.
But once the reader hits a ResourceDictionary, Baml2006Reader.Member.Name is "DeferrableContent" and Baml2006Reader.Value contains a MemoryStream that can not be parsed by another instance of Baml2006Reader. I can't event instantiate the reader:
System.IO.EndOfStreamException occurred HResult=-2147024858
Message=Unable to read beyond the end of the stream. Source=mscorlib StackTrace: at System.IO.MemoryStream.InternalReadInt32() at System.Windows.Baml2006.Baml2006Reader.Process_Header() at WpfApplication10.AssemblyExtensions.Read(Stream stream, List`1 result) in d:\Documents\Visual Studio 2012\Projects\WpfApplication10\WpfApplication10\AssemblyExtensions.cs:line 84 InnerException:
It seems that whenever the Baml2006Reader encounters an element where
Baml2006Reader.Member.Nameis"DeferrableContent"it is followed by another node whereBamlReader.Valueis aMemoryStream. It seems that this stream only contains a baml fragment and does not have a header (that's whySystem.Windows.Baml2006.Baml2006Reader.Process_Header()fails.)So we need to tell the baml reader to read a baml fragment. This can be done be giving the reader an instance of
System.Windows.Baml2006.Baml2006ReaderSettingswhere theIsBamlFragmentproperty istrue.Unfortunately both the
Baml2006ReaderSettingsclass and the appropriate constructor ofBaml2006Readerare internal. So we need to resort to reflection:usage:
I know this is rather fragile code and very hackish, but what the heck - it works (for me, currently)!