如何强制在 WPF DLL 中使用特定的 .resx 文件?

发布于 2024-08-11 06:02:59 字数 327 浏览 1 评论 0原文

我有一个从非托管 DLL 调用的 WPF DLL。 WPF DLL 有一个已翻译的对话框(两组 .resx 文件)。

如果我从 WinForm shell 或另一个 WPF shell 调用 WPF DLL,我可以通过设置当前线程的区域性来强制对话框使用特定语言(.resx 文件)。

但是,当从 C++ DLL 调用 WPF DLL(通过互操作 - COM)时,我无法让 WPF 对话框切换到默认语言以外的任何语言。

我不一定需要读取当前的系统区域性,因为非托管 DLL 的做法有所不同。我想告诉 WPF DLL 当我运行它时使用什么语言。

如何强制它在运行时加载特定语言?

I have a WPF DLL being called from an unmanaged DLL. The WPF DLL has a dialog that has been translated (two sets of .resx files).

If I call the WPF DLL from a WinForm shell, or another WPF shell, I can force the dialog to a particular language (.resx file) by setting the Culture of the current thread.

However, when calling the WPF DLL (through interop - COM) from the C++ DLL, I can't get the WPF dialog to switch to any language other than the default.

I don't necessarily need to read the current system culture, because the unmanaged DLL does it differently. I would like to tell the WPF DLL what language to use when I run it.

How can I force it to load with a particular language at runtime?

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

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

发布评论

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

评论(1

雾里花 2024-08-18 06:02:59

可以尝试的方法:

在一次调用中设置区域性并创建对话框,如下所示:

// Managed code
void SetCultureAndShowWindow(CultureInfo culture, ... more parameters for creating window ...)
{
  Thread.CurrentThread.CurrentCulture = culture;
  Window window = new Window(...
  window.ShowDialog();
}

通过互操作从 C++ 调用时,NET Framework 必须将本机线程绑定到 NET Framework 线程。我不记得细节了,但我记得有关 NET Framework 线程重用和垃圾收集的机制的一些信息。如果您正在调用设置区域性的托管代码,并第二次调用来创建窗口,则可能的情况是:

  1. 您调用托管代码来设置区域性。
  2. 创建一个新的托管线程
  3. 您的调用返回,并且托管线程被释放。
  4. 您调用托管代码来创建并显示窗口。
  5. 创建了一个新的托管线程
  6. 窗口显示了错误的区域性

这种事件序列是可以想象的,具体取决于托管本机线程绑定代码的实现。因此,我建议您尝试在一次调用中执行这两项操作,看看它是否会改变任何内容。

Something to try:

Set the culture and create the dialog in a single call, like this:

// Managed code
void SetCultureAndShowWindow(CultureInfo culture, ... more parameters for creating window ...)
{
  Thread.CurrentThread.CurrentCulture = culture;
  Window window = new Window(...
  window.ShowDialog();
}

When calling from C++ through interop, NET Framework must bind your native thread to a NET Framework thread. I don't remember the details, but I remember something about a mechanism where NET Framework threads are reused and garbage collected. If you are making a call to managed code that is setting the culture and a second call to create the window, a possible scenario is:

  1. You call the managed code to set the culture.
  2. A new managed thread is created
  3. Your call returns, and the managed thread is released.
  4. You call the managed code to create and show the window.
  5. A new managed thread is created
  6. The window shows with the wrong culture

Such a sequence of events is conceivable, depending on the implementation of the mangaged-native thread binding code. Therefore I suggest you try doing both in a single call to see if it changes anything.

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