Detecting if IME (Input Method Editor) is active in Silverlight
I was able to resolve the issue in both the WPF and Silverlight frameworks. The issue was caused by the fact that by handling the TextBox Text while a IME is inputting symbols that Text was making the IME itself change its input which it looks like is not handled gracefully by the Windows OS and was causing a CLR exception.
What I did was:
In the WPF framework as mentioned I used the static InputMethod.Current.ImeState value to determine if IME is active and if it was with On value I skipped reverting the TextBox Text property in its TextChanged event.
In the Silverlight framework I use a combination of the TextInputStart, TextInputUpdate events and a local private field to store if IME was detected. The TextInputUpdate event is only triggered if IME is active and used as input and the TextInputStart is always triggered. What I did was:
- Created a bool IsImeActive = false; filed
- Hook to the TextInputStart event of the TextBox
- In that event set the IsImeActive field to False
- Hook to the TextInputUpdate event of the TextBox
- In that event set the IsImeActive field to True
- Finally in the TextChanged event add a condition that checks the IsImeActive field and if it is False run the logic which handles (reverses) the input.
Hope this is helpful.