Multilingual graphical user interface WPF Switch-on-fly

resource files have already been created. swtich language after restarting the application is currently working.

Is it possible to switch the gui language (tape) on the fly?

already tried: change cultureinfo and call initializcomomponents does not work.

+3
source share
2 answers

I bounced off this topic too early. I have found that this is possible, but it will take some effort. The only solution that I remember about is that you left your application. At this point, a package is launched that will restart your application so that the user does not worry when the application suddenly leaves and disappears. When your application restarts, it will reload all ui elements with the appropriate culture.

I did not use this method because I do not like to quit my application to make changes. But it is possible.

What you are experiencing is that the culture has changed in your application, but your controls are not updated accordingly. Restarting fixes this.

Hope this helps a bit.

+1
source

, . , - . .Net 4.5.

:

  • , <TextBlock Text="{Binding Source={x:Static p:Resources.LocalizedString}}"/> <TextBlock Text="{Binding Path=(p:Resources.LocalizedString)}"/>. ( . XAML .)

  • . , :

    // ... after the current culture has changed
    UpdateStaticBindings(Application.Current.MainWindow, typeof(Properties.Resources), true);
    
    /// <summary>
    /// Update all properties bound to properties of the given static class.
    /// Only update bindings like "{Binding Path=(namespace:staticClass.property)}".
    /// Bindings like "{Binding Source={x:Static namespace:staticClass.property}}" cannot be updated.
    /// </summary>
    /// <param name="obj">Object that must be updated, normally the main window</param>
    /// <param name="staticClass">The static class that is used as the binding source, normally Properties.Resources</param>
    /// <param name="recursive">true: update all child objects too</param>
    static void UpdateStaticBindings(DependencyObject obj, Type staticClass, bool recursive)
    {
        // Update bindings for all properties that are statically bound to
        // static properties of the given static class
        if (obj != null)
        {
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(obj);
    
            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.DependencyProperty != null)
                    {
                        BindingExpression be = BindingOperations.GetBindingExpression(obj, mp.DependencyProperty) as BindingExpression;
    
                        if (be != null)
                        {
                            // Only update bindings like "{Binding Path=(namespace:staticClass.property)}"
                            if (be.ParentBinding.Path.PathParameters.Count == 1)
                            {
                                MemberInfo mi = be.ParentBinding.Path.PathParameters[0] as MemberInfo;
                                if (mi != null && mi.DeclaringType.Equals(staticClass))
                                {
                                    be.UpdateTarget();
                                }
                            }
                        }
                    }
                }
            }
    
            // Iterate children, if requested
            if (recursive)
            {
                foreach(object child in LogicalTreeHelper.GetChildren(obj))
                {
                    UpdateStaticBindings(child as DependencyObject, staticClass, true);
                }
            }
        }
    }
    
0

Source: https://habr.com/ru/post/1711688/


All Articles