语言更改后立即访问 CurrentInputLanguage 属性不会更新

Posted

技术标签:

【中文标题】语言更改后立即访问 CurrentInputLanguage 属性不会更新【英文标题】:CurrentInputLanguage Property does not update when accessed right after language change 【发布时间】:2016-04-14 22:46:53 【问题描述】:

请查看下面的代码以更好地理解我的意思:

using System;
using System.Windows.Forms;

namespace CurrentInputLanguageTest

    static class Program
    
        [STAThread]
        static void Main()
        
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's US
            Console.ReadLine(); // Changed my keyboard layout while typing something
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's still US

            var form = new Form();
            var button = new Button();
            button.Click += CheckInputLanguage;
            form.Controls.Add(button);
            Application.Run(form);
        

        static void CheckInputLanguage(object sender, EventArgs e)
        
            // I have changed my input language while the form is opened and pressed the button.
            // It changes when called in this event handler.
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); 
        
    

问题:我的应用中有一些事件处理程序,我需要知道触发事件时当前的输入语言是什么。我该怎么做?

【问题讨论】:

【参考方案1】:

它总是返回 US 的原因是因为您在命令中并且仅针对该输入行更改了设置。

如果用户在显示表单时更改输入语言,则正确返回当前语言。但你也可以用另一个按钮以编程方式设置:

using System;
using System.Windows.Forms;

namespace CurrentInputLanguageTest

    static class Program
    
        [STAThread]
        static void Main()
        
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's US
            Console.ReadLine(); // Changed my keyboard layout while typing something
            Console.WriteLine(Application.CurrentInputLanguage.LayoutName); // It's still US

            var form = new Form();
            var grid = new TableLayoutPanel();
            var button = new Button();
            var button2 = new Button();
            button2.Left = button.Right + 5;
            form.Refresh();
            button.Click += CheckInputLanguage;
            button2.Click += ChangeInputLanguage;
            form.Controls.Add(button);
            form.Controls.Add(button2);
            Application.Run(form);
        

        private static void ChangeInputLanguage(object sender, EventArgs e)
        
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
        

        static void CheckInputLanguage(object sender, EventArgs e)
        
            // I have changed my input language while the form is opened and pressed the button.
            // It changes when called in this event handler.
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName);
        
    

【讨论】:

以上是关于语言更改后立即访问 CurrentInputLanguage 属性不会更新的主要内容,如果未能解决你的问题,请参考以下文章

更改数据库数据或重新查询后访问 2007 VBA 列表框滞后

插入后不会立即更改 Blazor 文本

使用本机反应更改相机类型后立即拍照

如何在使用 QWebEngineView 'loadFinished' 加载页面后立即更改 html 元素?

UICollectionViewCell 在滚动开始后立即更改数据

MS Access - 更改表单中的值后立即写入表