简体   繁体   中英

how to reload all localized string in a WPF window?

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 .

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

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 ax:name set the value in the code bhind to the new value manually:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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