.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Help with InputManager, InputLanguageChanged event
 

Help with InputManager, InputLanguageChanged event

I'm working on a WPF application and I need to be able to handle the InputLanguageChanged event when the user switches their input language.

I'm lost when it comes to events at the moment, could someone please point me to an example of how to write a handler for the event?

heasty
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
  • Marked As Answer byheasty Wednesday, September 23, 2009 11:06 AM
  •  
Bigsby
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
  • Marked As Answer byheasty Wednesday, September 23, 2009 11:06 AM
  •  
Bigsby
THANK YOU!

That was exactly what I needed. Thank you so much for taking the time to answer this with such clarity.
heasty

You can use google to search for other answers

Custom Search

More Threads

• Visual tree updates
• Update Points of Canvas' Children on Transform
• Going to VS Live in Las Vegas this week?
• System.ArithmeticException: Overflow or underflow in the arithmetic operation.
• How to get width of columnDefinition as gridUnitType star
• Executable Path
• Vs 2005 & framework 3.5 with WPF
• best practices to manage vector icons referenced across an application?
• positioning cursor in a text box
• Avoiding XamlWriter.Save() - how to get a list of changed properties