如何在 WPF 窗口中重新加载所有本地化字符串?

发布于 2024-12-10 15:00:16 字数 1611 浏览 0 评论 0原文

我有一个 WPF 窗口,许多标签都使用本地化字符串。例如:

<Window x:Class="CoHOLauncher.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:l="clr-namespace:MyNameSpace"
        xmlns:loc="clr-namespace:MyNameSpace.Localization">

<Label Grid.Column="0" Content="{x:Static loc:Strings.MainWindowSize}" HorizontalAlignment="Right"/>

Strings 类的详细信息如下:

public class Strings {
        private static global::System.Globalization.CultureInfo resourceCulture;
        public static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MyNameSpace.Localization.Strings", typeof(Strings).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

    public static global::System.Globalization.CultureInfo Culture {
        get {
            return resourceCulture;
        }
        set {
            resourceCulture = value;
        }
    }

        public static string MainWindowSize {
            get {
                return ResourceManager.GetString("MainWindowSize", resourceCulture);
            }
        }
}

如果我在运行时更改了 Strings.Culture,如何强制 WPF 重新加载所有字符串?

我将所有字符串存储在本地化卫星 DLL 程序集中,这些程序集是从本地化资源文件生成的。

I have a WPF window and many labels are using localized strings. For example :

<Window x:Class="CoHOLauncher.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:l="clr-namespace:MyNameSpace"
        xmlns:loc="clr-namespace:MyNameSpace.Localization">

<Label Grid.Column="0" Content="{x:Static loc:Strings.MainWindowSize}" HorizontalAlignment="Right"/>

Details of the Strings class as follow :

public class Strings {
        private static global::System.Globalization.CultureInfo resourceCulture;
        public static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MyNameSpace.Localization.Strings", typeof(Strings).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

    public static global::System.Globalization.CultureInfo Culture {
        get {
            return resourceCulture;
        }
        set {
            resourceCulture = value;
        }
    }

        public static string MainWindowSize {
            get {
                return ResourceManager.GetString("MainWindowSize", resourceCulture);
            }
        }
}

If I changed Strings.Culture in run time, how to force WPF to reload all strings ?

I store all strings in a localization satellite DLL assemblies , which are generated from localized resource files .

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

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

发布评论

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

评论(2

活泼老夫 2024-12-17 15:00:16

您必须更改 x:Static 值的绑定源,并通过提供值更改通知的属性以某种方式公开本地化字符串。

You would have to change the binding source from an x:Static value and somehow expose the localized strings through a property that provides value change notifications.

与风相奔跑 2024-12-17 15:00:16

静态绑定似乎只是对字符串的绑定。换句话说,看起来,当您绑定到静态变量时,wpf 只是设置相关属性的值,而忘记绑定静态元素(因为 wpf“认为”该值永远不会改变。

)要做的就是让 xaml 元素 ax:name 手动将后面代码中的值设置为新值:

// 给定事件的代码隐藏:
取消.内容=属性.登录.密码;

Static binding seems to be simply a binding to the string. In other words, it looks like, when you bind to a static variable, wpf just sets the value for the property in question and forget about the binding static element (since wpf "thinks" that the value will never change.)

What you can do is to have the xaml element a x:name set the value in the code bhind to the new value manually:

// code behind for a given event:
cancel.content = Properties.login.password;

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