以编程方式将资源字典保存到磁盘

时间:2012-05-03 08:51:41

标签: wpf resources save

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

我找不到直接的方法。有什么像ResurceDictionary.Save()?

2 个答案:

答案 0 :(得分:2)

您可以使用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);

答案 1 :(得分:1)

ResourceDictionary dic = Application.Current.Resources;

StreamWriter writer = new StreamWriter("Themes\\NewOne.xaml");
XamlWriter.Save(dic, writer);
writer.Close();