简体   繁体   English

在所有窗体上使用KeyDown事件更改语言

[英]Language change with KeyDown event on all Form

I develop an application that can change language between english and hungarian. 我开发了可以在英语和匈牙利语之间切换语言的应用程序。 This software work with .resx files and when the user presses F2 the Labels, Buttons, and other controls their Text properties will be changed from the .resx file. 该软件适用于.resx文件,当用户按F2键时,标签,按钮和其他控件的Text属性将从.resx文件中更改。

It's ok, but when I have more than one Form open, naturally the KeyDown event changes those controls that are in the focused Form. 没关系,但是当我打开多个Form时, KeyDown事件自然会更改聚焦Form中的那些控件。 So my question is: how can I capture the F2 keystroke in all opened Forms? 所以我的问题是:如何在所有打开的窗体中捕获F2击键?

You can probably use Events to implement this. 您可能可以使用事件来实现此目的。 I don't know which technology you are using. 我不知道您正在使用哪种技术。 WinForms or WPF. WinForms或WPF。

IF you are developing this functionality in a WPF technology with MVVM then you could have declared 'SelectedLanguage' property in a Class lets say 'Langugage' implementing INotifyPropertyChanged interface. 如果您正在使用MVVM在WPF技术中开发此功能,那么您可以在实现INotifyPropertyChanged接口的类“ Langugage”中声明“ SelectedLanguage”属性。 Derive your other view models with this class and then on 'SelectedLanguage' propertyChanged event you can notify each and every view model about the change of language on which you can have a method to read resource files and change values of UI. 使用此类派生其他视图模型,然后在“ SelectedLanguage” propertyChanged事件中,可以通知每个视图模型有关语言的更改,您可以使用该方法来读取资源文件和更改UI值。

May be I am out of context with respect to technology. 可能是我在技术方面没有背景。 But Events mechanism or Observer Pattern will resolve your problems. 但是事件机制或观察者模式将解决您的问题。

Note: Based on clues in your question I am assuming Winforms rather than WPF: 注意:根据您问题的线索,我假设使用Winforms而不是WPF:

how can I capture the F2 keystroke in all opened Forms? You can't, not without some ridiculously complicated code. 您不能没有一些可笑的复杂代码。 The closest you can come is by re-dispatching events in one Form to other forms. 您可以通过将事件以一种形式重新分发给其他形式来得到。 In any case, you need a way to track your open windows and when an F2 key is hit in any of them you need to tell all of your open forms to change the language. 无论如何,您都需要一种跟踪打开的窗口的方法,当在其中任何一个窗口中按下F2键时,您都需要告诉所有打开的窗体以更改语言。

Somewhere in your application where you create new forms you need a collection like List<Form> for example that will hold your list of existing forms. 在您的应用程序中创建新表单的某个地方,您需要一个类似List<Form>的集合,该集合将保存现有表单的列表。 (You can also try and use built-in collections in your Control objects) Whenever you create a new form, you add to this collection, when one closes you should remove it. (您也可以尝试在Control对象中使用内置集合)。无论何时创建新表单,都将添加到该集合中,关闭该表单时应将其删除。 And when any of those forms captures an F2 key then you loop through the collection telling each one to switch its language. 而且,当这些形式中的任何一个捕获到F2键时,您便会遍历该集合,并告诉每个窗口切换其语言。

This would be a great place for you to inherit from the Form class and add your own properties and methods to make the behavior uniform across all of your forms. 这是从Form类继承并添加自己的属性和方法以使行为在所有表单中统一的好地方。

Here is an example article that uses delegates for inter-form communication. 这是示例示例,示例使用委托进行表单间的通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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