x:Static 类型转换器何时进行评估:运行时还是编译时?

发布于 2024-10-31 13:14:05 字数 1105 浏览 0 评论 0原文

我有一个 wpf 应用程序,我开始对其进行本地化。我选择的本地化策略是创建自定义 MarkupExtension 类。

我不会使用 .res 文件,因为在我的公司已经有一个解决方案,它创建一个包含所有字符串的加密映射,并且需要 int 键来引用每个字符串。所以我编写了一个自定义生成器,它创建一个带有所有“int”键的枚举(可能是带有整数的静态类,这并不重要)。

因此,我想在我的 XAML 文件中引用每个枚举键,执行以下操作:

<Label Content="{l:Translator {x:Static l:TranslatedEnums.MainWindow_WelcomeMessage}}" />

创建的枚举是 TranslatedEnums,我的翻译器类称为 Translator (dhu)。

但阅读 x:Static 文档类型转换器文档,我想到了以下问题:

“编译的 xaml”(baml) 实际上是否在运行时或编译时评估每种类型(通过 x:Static)?

我问这个是因为我提到的原因之一枚举值是静态的,因此我没有确切的枚举字符串键,而是它的值,这将使破解应用程序变得更加困难。

如果解析是在运行时完成的,那么我可以将之前的代码缩小为:

<Label Content="{l:Translator MainWindow_WelcomeMessage}" />

这将需要我自己查找枚举值,但这是一行:

TranslatedEnums result;
Enum.TryParse(key, out result);

这将使 Xaml 代码更小(这很好) ,但如果密钥不存在(这不是问题),它会在编译时失败。

I have a wpf application and I'm starting to localize it. The strategy for localization I chose was to create my custom MarkupExtension class.

I won't be using the .res files since at my company there is a solution that is already made which creates an encrypted map with all the strings and it requires int keys to refer to each string. So I wrote a custom generator that creates an enum with all the 'int' keys (could be a static class with ints, it doesn't really matter).

So I want to refer to each of these enum keys at my XAML files, doing:

<Label Content="{l:Translator {x:Static l:TranslatedEnums.MainWindow_WelcomeMessage}}" />

The created enum is TranslatedEnums and my translator class is called Translator (dhu).

But after reading the x:Static documentation and the type converters documentation, the following question came to my mind:

Does the "compiled xaml" (baml) actually evaluates each of these types (via x:Static) at runtime or compile-time?

I'm asking this because one of the reasons I'm referring to the enum value statically is so that I don't have the exact enum string key, but rather it's value, which would make hacking the application a little bit harder.

If the resolution is done at runtime, then I could narrow my previous code to:

<Label Content="{l:Translator MainWindow_WelcomeMessage}" />

Which would require the lookup for the enum value by my own, but this is a one-liner:

TranslatedEnums result;
Enum.TryParse(key, out result);

Which would make the Xaml code smaller (which is good), but it would fail at compile-time if the key didn't exist (which is not a problem).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风铃鹿 2024-11-07 13:14:05

它实际上是在 xaml 加载时(如此处所述) )。如果您只加载一次 xaml,则可以在运行时考虑它。

It's actually at xaml load-time (as stated here). if you only load your xaml onece, you could consider it at run-time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文