I'm making a theme editor for a WPF application. I generate XAML files dynamically, then I compile them into a DLL that is used by the application. The code used to generate the XAML files goes along those lines:
var dictionary = new ResourceDictionary();
...
dictionary.Add(key, new BitmapImage { UriSource = new Uri(relativePath, UriKind.Relative) });
...
XamlWriter.Save(dictionary, "myDictionary.xaml");
My problem is that XamlWriter.Save
also serializes the BaseUri
property:
<BitmapImage BaseUri="{x:Null}" UriSource="Images\myImage.png" x:Key="myImage" />
The result is that when the application tries to fetch this image, it doesn't find it because BaseUri
is not set. Normally the XAML parser sets this property (through the IUriContext
interface), but when it is already set explicitly in XAML, the parser doesn't set it, so it remains null.
Is there a way to prevent the XamlWriter
from serializing the BaseUri
property?
If I was serializing a custom class, I could add a ShouldSerializeBaseUri()
method, or implement IUriContext
explicitly (I tried both options and they give the desired result), but how to do it for BitmapImage
?
As a last resort, I could load the XAML file and remove the attribute with Linq to XML, but I was hoping for a cleaner solution.