Windows10下用国际键盘时WPF .NET3.5应用崩溃求助
.NET 3.5 WPF应用在Windows 10英语国际键盘下输入崩溃的问题及临时解决方案
问题现象
在Windows 10系统中测试基于.NET 3.5的WPF应用时,若系统设置为英语国际键盘,在文本框执行输入操作时应用会直接崩溃,抛出错误:
Culture ID 8192 (0x2000) is not a supported culture.
该问题无法在Windows 7系统上复现,切换为其他键盘布局即可正常使用应用。
错误堆栈跟踪(法语)
崩溃的堆栈跟踪直接指向微软内部代码,具体信息如下:
System.ArgumentException: L'ID de culture 8192 (0x2000) n'est pas une culture prise en charge. Nom du paramètre : culture à System.Globalization.CultureTableRecord.GetCultureTableRecord(Int32 cultureId, Boolean useUserOverride) à System.Globalization.CultureInfo..ctor(Int32 culture, Boolean useUserOverride) à System.Windows.Input.InputLanguageSource.get_CurrentInputLanguage() à System.Windows.Input.InputLanguageManager.get_CurrentInputLanguage() à System.Windows.Documents.TextSelection.EnsureCaret(Boolean isBlinkEnabled, CaretScrollMethod scrollMethod) à System.Windows.Documents.TextSelection.System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight() à System.Windows.Documents.TextEditor.OnGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e) à System.Windows.Controls.Primitives.TextBoxBase.OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) à System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) à System.Windows.Input.InputManager.ProcessStagingArea() à System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) à System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp) à System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew) à System.Windows.Input.KeyboardDevice.Focus(IInputElement element) à System.Windows.UIElement.Focus() à System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(TextEditor This) à System.Windows.Documents.TextEditorMouse.OnMouseDown(Object sender, MouseButtonEventArgs e) à System.Windows.Controls.Primitives.TextBoxBase.OnMouseDown(MouseButtonEventArgs e) à System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e) à System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) à System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) à System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) à System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) à System.Windows.Input.InputManager.ProcessStagingArea() à System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) à System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) à System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) à System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) à System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) à MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) à MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) à System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) à System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) à System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) à MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) à MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) à System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG& msg) à System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) à System.Windows.Application.RunInternal(Window window) à MyApp.App.Main()
解决方向与临时方案
- 彻底解决(但有兼容性限制):将应用的目标框架切换为.NET 4.0即可消除该问题,但如果需要兼容旧系统(如Windows XP),则无法采用此方案。
- 临时解决方案:在应用的
OnStartup(StartupEventArgs e)方法中添加检测逻辑,若当前文化设置会引发异常,则提示用户切换键盘布局并关闭应用,示例代码思路如下:
protected override void OnStartup(StartupEventArgs e) { try { // 尝试获取当前输入语言,触发潜在的文化异常 var currentLang = System.Windows.Input.InputLanguageManager.Current.CurrentInputLanguage; } catch (ArgumentException ex) { // 捕获到文化不支持的异常时提示用户 MessageBox.Show("当前键盘布局不被支持,请切换为其他键盘布局后重新启动应用。", "兼容性提示", MessageBoxButton.OK, MessageBoxImage.Warning); // 关闭应用 Shutdown(); return; } base.OnStartup(e); }
内容的提问来源于stack exchange,提问作者Sylvain B.
相关产品推荐
相关产品推荐

