简体   繁体   English

(C#,WPF)在本地保存ResourceDictionary

[英](C#,WPF) Save a ResourceDictionary locally

everyone! 大家!

I post a request for help here because I'm blocked. 我在这里发布了帮助请求,因为我被阻止了。

I made a translation system using Resource Dictionaries for keys and values. 我使用Resource Dictionary编写了键和值的翻译系统。 Nothing to complicate. 没什么复杂的。

Except now I would like to save my Dictionaries locally, (at the launch of the software) so on the disk in a .XAML file. 除了现在,我想将我的词典保存在本地(在软件启动时),以便保存在磁盘上的.XAML文件中。

The problem is that I can't find a way to save them 问题是我找不到保存它们的方法

Here is a resource dictionary that I would like to save: 这是我要保存的资源字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:sys="clr-namespace:System;assembly=mscorlib">

<!-- General -->
<sys:String x:Key="LanguageName">en</sys:String>
<sys:String x:Key="LanguageDisplayName">English</sys:String></ResourceDictionary>

Thanks & have a good day! 谢谢,祝你有美好的一天! And sorry for my approximate English, I am French :x 很抱歉,我的英语差不多,我是法语:x

Yes of course you can write files to disk. 是的,您当然可以将文件写入磁盘。 And yes you can then later read them back in to re-create a resource dictionary. 是的,您可以稍后再读回它们以重新创建资源字典。

I'm not clear how the user receives what you're showing us. 我不清楚用户如何收到您向我们展示的内容。

If it's as a compiled resource dictionary you can instantiate that and then write it to disk using XamlWriter. 如果它是已编译的资源字典,则可以实例化该字典,然后使用XamlWriter将其写入磁盘。

https://docs.microsoft.com/en-us/dotnet/api/system.windows.markup.xamlwriter?view=netframework-4.7.2 https://docs.microsoft.com/en-us/dotnet/api/system.windows.markup.xamlwriter?view=netframework-4.7.2

Or you could read a "flat" uncompiled xaml file off a web server and save it to appdata. 或者,您可以从Web服务器上读取“平面”未编译的xaml文件,然后将其保存到appdata中。

An odd thing about resource dictionaries. 关于资源词典的一件奇怪的事。

The same syntax you use to merge a resource dictionary will work with an uncompiled .xaml file. 用于合并资源字典的语法与未编译的.xaml文件一起使用。

https://social.technet.microsoft.com/wiki/contents/articles/28797.wpf-dynamic-xaml.aspx#Loading_a_Flat_File_as_a_Resource_Dictionary https://social.technet.microsoft.com/wiki/contents/articles/28797.wpf-dynamic-xaml.aspx#Loading_a_Flat_File_as_a_Resource_Dictionary

You can alternatively xamlreader.load() a flat .xaml file and create a resource dictionary in memory that you then merge in code. 您可以选择xamlreader.load()一个.xaml文件,并在内存中创建资源字典,然后将其合并到代码中。

You can manipulate uncompiled xaml files as strings or treat them as xml. 您可以将未编译的xaml文件处理为字符串,也可以将其视为xml。 Some of the samples in the link do that. 链接中的一些示例可以做到这一点。

And you can receive a resource dictionary as a string from a service and xamlreader.parse() that into a resource dictionary in memory. 您可以从服务中将资源字典作为字符串接收,并将xamlreader.parse()接收到内存中的资源字典中。 That you can then merge in code. 然后可以合并代码。

BTW BTW

Your English seems fine to me. 您的英语对我来说似乎还不错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM