我需要使用位于本地计算机上的资源字典。我的WPF应用程序将使用Add()或Remove()方法动态地向该字典添加/删除项。完成后,我需要将更新后的资源字典再次保存到磁盘中。
我没有找到直接的方法。是否有类似于ResourceDictionary.Save()的东西?
我没有找到直接的方法。是否有类似于ResourceDictionary.Save()的东西?
您可以使用 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);
ResourceDictionary dic = Application.Current.Resources;
StreamWriter writer = new StreamWriter("Themes\\NewOne.xaml");
XamlWriter.Save(dic, writer);
writer.Close();