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

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

StringBuilder出了什么问题? - vidstige

2
ResourceDictionary dic = Application.Current.Resources;

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接