You can write handlers for events in many ways. You can choose depending on how you want to keep your code clean and readable.
Theory:
- A delegate is a definition for methods regarding their ins and outs. Lamely, its says: Delegates of this type receive this and return that.
- An event handler is a certain defined delegate type. Usually, an object that is the sender instance where the event was raised and same type of predicted argumenst.
In the case of InputLanguageChanged event its is handled by delegates of InputLanguageEventHandler type by whose definition the method receives an object and InputLanguageEventArgs that hold two CultureInfo objects, namely, PreviousLanguage and NewLanguage.
Adding a handler to an event.
The usual way
public Window1()
{
InitializeComponent();
InputLanguageManager.Current.InputLanguageChanged += new InputLanguageEventHandler(Current_InputLanguageChanged);
}
void Current_InputLanguageChanged(object sender, InputLanguageEventArgs e)
{
//Do what ever you want here
}
Using this method if Visual Sudio you can hit Tab after "+=" and the method will be automatically generated.
Many people use this and then clean the code like so:
InputLanguageManager.Current.InputLanguageChanged += Current_InputLanguageChanged;
That does exactly the same as above but is a lot cleaner.
Now, if you don't want to reuse the code that is run in the event, there are other ways to make it look cleaner and, obviously, with the same practical runtime effect.
Anonymous method:
InputLanguageManager.Current.InputLanguageChanged += delegate(object sender, InputLanguageEventArgs e)
{
//Do what ever you want here
};
And, my favorite, a Lambda Expression:
InputLanguageManager.Current.InputLanguageChanged += (sender, e) =>
{
//Do what ever you want here
};
Hope this isn't to much nor too little.
Bigsby, Lisboa, Portugal -
O que for, quando for, é que será o que é...
http://bigsby.eu