Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF多语言GUI动态切换_Wpf_Multilingual - Fatal编程技术网

WPF多语言GUI动态切换

WPF多语言GUI动态切换,wpf,multilingual,Wpf,Multilingual,已创建资源文件。 重新启动应用程序后,切换语言当前有效 是否可以动态切换gui(功能区)的语言 已尝试: 更改cultureinfo和调用initializecomponents不起作用。我刚才也谈到了这个话题。我发现这是可能的,但需要一些努力。我记得唯一的解决办法是退出应用程序。此时会启动一个批处理,它将重新启动您的应用程序,这样当应用程序突然退出并消失时,用户不会惊慌失措。重新启动应用程序后,它将重新加载具有适当区域性的所有ui元素 我没有使用这种方法,因为我不喜欢为了进行更改而退出我的应用

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

是否可以动态切换gui(功能区)的语言

已尝试:
更改cultureinfo和调用initializecomponents不起作用。

我刚才也谈到了这个话题。我发现这是可能的,但需要一些努力。我记得唯一的解决办法是退出应用程序。此时会启动一个批处理,它将重新启动您的应用程序,这样当应用程序突然退出并消失时,用户不会惊慌失措。重新启动应用程序后,它将重新加载具有适当区域性的所有ui元素

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

您所经历的是,应用程序中的文化确实发生了变化,但您的控件没有相应地更新。重启“修复”了这个问题


希望这能有所帮助。

我也遇到过同样的问题,并找到了一个适合我的解决方案。它不需要转换器,但仍然需要一段代码。使用.NET4.5框架

要在运行中切换语言,需要两件不明显的事情:

  • 使用另一种绑定静态属性的方式,替换
    带有
    。(带括号的绑定语法用于附加属性和静态属性。作为副作用,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);
                }
            }
        }
    }
    
    /。。。在当前文化发生变化之后
    UpdateStaticBindings(Application.Current.MainWindow,typeof(Properties.Resources),true);
    /// 
    ///更新绑定到给定静态类的属性的所有属性。
    ///仅更新绑定,如“{Binding Path=(命名空间:staticClass.property)}”。
    ///无法更新像“{Binding Source={x:Static namespace:staticClass.property}}”这样的绑定。
    /// 
    ///必须更新的对象,通常是主窗口
    ///用作绑定源的静态类,通常为Properties.Resources
    ///true:也更新所有子对象
    静态void UpdateStaticBindings(DependencyObject obj,类型staticClass,bool recursive)
    {
    //更新静态绑定到的所有属性的绑定
    //给定静态类的静态属性
    如果(obj!=null)
    {
    MarkupObject MarkupObject=MarkupWriter.GetMarkupObjectFor(obj);
    if(markupObject!=null)
    {
    foreach(markupObject.property中的MarkupProperty mp)
    {
    if(mp.dependencProperty!=null)
    {
    BindingExpression be=BindingOperations.GetBindingExpression(obj,mp.DependencyProperty)作为BindingExpression;
    如果(be!=null)
    {
    //仅更新绑定,如“{Binding Path=(命名空间:staticClass.property)}”
    if(be.ParentBinding.Path.PathParameters.Count==1)
    {
    MemberInfo mi=be.ParentBinding.Path.PathParameters[0]作为MemberInfo;
    if(mi!=null&&mi.DeclaringType.Equals(staticClass))
    {
    be.UpdateTarget();
    }
    }
    }
    }
    }
    }
    //如果需要,迭代子项
    if(递归)
    {
    foreach(LogicalTreeHelper.GetChildren(obj)中的对象子对象)
    {
    UpdateStaticBindings(子对象作为DependencyObject,staticClass,true);
    }
    }
    }
    }
    

  • 您使用什么技术进行本地化?洛巴姆?Resx文件?还有别的吗?