3

我需要使用位于本地计算机中的资源字典。我的 WPF 应用程序将使用 Add() 或 Remove() 方法向该字典动态添加/删除项目。完成后,我需要将更新的资源字典再次保存到磁盘。

我没有找到直接的方法。有没有像 ResurceDictionary.Save() 这样的东西?

4

2 回答 2

3

你可以使用XamlWriter类。

这是一段将按钮的模板写入文件的代码:

// Get the template.
ControlTemplate template = button.Template;

// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(@"c:\template.xaml", settings);
XamlWriter.Save(template, writer);
于 2012-05-03T10:48:56.013 回答
2
ResourceDictionary dic = Application.Current.Resources;

StreamWriter writer = new StreamWriter("Themes\\NewOne.xaml");
XamlWriter.Save(dic, writer);
writer.Close();
于 2013-01-05T15:58:48.077 回答