如何prevent XamlWriter.Save从序列化的基本URI的财产?财产、基本、序列化、XamlWriter

由网友(早已不年少 ")分享简介:我在做一个主题编辑器,一个WPF应用程序。我动态生成XAML文件,然后我编译成所使用的应用程序的DLL。用于生成XAML文件的code去沿着这些线路:I'm making a theme editor for a WPF application. I generate XAML files dynamically,...

我在做一个主题编辑器,一个WPF应用程序。我动态生成XAML文件,然后我编译成所使用的应用程序的DLL。用于生成XAML文件的code去沿着这些线路:

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");

我的问题是, XamlWriter.Save 也序列化基本URI 属性:

<BitmapImage BaseUri="{x:Null}" UriSource="ImagesmyImage.png" x:Key="myImage" />

其结果是,当应用程序试图获取这个形象,它没有找到它,因为基本URI 未设置。通常情况下,XAML分析器设置此属性(通过 IUriContext 接口),但是当它已经被设置明确XAML,解析器不设置它,所以它仍然空。

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.

有没有一种方法,以prevent的的XamlWriter 从序列化基本URI 属性

Is there a way to prevent the XamlWriter from serializing the BaseUri property?

如果我被序列化自定义类,我可以添加一个 ShouldSerializeBaseUri()方法,或实施 IUriContext 明确(我想这两个选项,他们得到所需的结果),但如何做到这一点的的BitmapImage

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?

作为最后一招,我可以加载XAML文件,并使用LINQ删除属性为XML,但我希望一个更清洁的解决方案。

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.

推荐答案

我最终被使用LINQ手动生成XAML到XML解决了这个问题。

I eventually solved the problem by generating the XAML manually with Linq to XML.

阅读全文

相关推荐

最新文章