3

资源文件已经创建。重新启动应用程序后切换语言当前有效。

是否可以即时切换 gui(Ribbon) 的语言?

已经尝试过:更改cultureinfo 并调用initializecomponents 不起作用。

4

2 回答 2

1

不久前我也谈到了这个话题。我发现这是可能的,但需要一些努力。我记得的唯一解决方案是您退出应用程序。在那一刻,将启动一个批处理,这将重新启动您的应用程序,因此当应用程序突然退出并消失时,用户不会惊慌失措。当您的应用重新启动时,它将重新加载具有适当文化的所有 ui 元素。

我没有使用这种方法,因为我不喜欢为了进行更改而不得不退出我的应用程序。但这是可能的。

您正在经历的是,您的应用程序中的文化确实发生了变化,但您的控件并未相应更新。重新启动“修复”此问题。

希望这会有所帮助。

于 2011-03-09T15:03:23.940 回答
0

I have encountered with the same problem and found a solution that works for me. It does not require converters, but a piece of code is still necessary. The .Net 4.5 framework is used.

Two unobvious things are needed in order to switch languages on-the-fly:

  1. Use another flavour of binding to static properties, replace <TextBlock Text="{Binding Source={x:Static p:Resources.LocalizedString}}"/> with <TextBlock Text="{Binding Path=(p:Resources.LocalizedString)}"/>. (The binding syntax with parentheses is used for attached properties and static properties. As a side effect, the XAML designer will show empty strings for these properties.)

  2. Change notifications do not work with resources. To workaround that, static bindings are manually refreshed by the following code:

    // ... 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);
                }
            }
        }
    }
    
于 2015-11-26T00:57:56.057 回答