[英]WPF app crash on Windows 10 when using international keyboard
在具有英语国际键盘设置的Windows 10上测试WPF .Net应用程序会导致应用程序崩溃,错误Culture ID 8192 (0x2000) is not a supported culture.
键入文本框时发生崩溃。
该错误无法在Windows 7上重现,并在设置另一个键盘时在Windows 10上解决。
目标框架是.Net 3.5。
stacktrace(用法语)对我帮助不大,它直接在微软的代码中:
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解决了这个问题。 但令人遗憾的是,我们必须以3.5为目标,以确保与此应用程序的旧操作系统兼容。
这个事实让这个问题很奇怪,但我想保持开放,万一有人可以提供一些帮助。
解决方案:这是一个转变而不是解决方案:我们在OnStartup(StartupEventArgs e)
添加了一些代码来警告用户文化设置是否引发异常并关闭应用程序:
try
{
CultureInfo cul = new CultureInfo(System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture.LCID, true);
}
catch
{
//warn the user...
Current.Shutdown();
}
我在我的程序调用的库中遇到了这个问题,没有任何摆弄Windows文化的东西似乎解决了它。
最后,在深入了解堆栈跟踪并确切地看到出现了什么问题之后,我想出了循环所有已安装的输入语言的解决方法,跳过那些具有“损坏的”文化,并设置System.Windows.Forms.InputLanguage.CurrentInputLanguage
保留最适用的一个。
可以专门检查键盘布局,因此第一个具有相同布局名称的布局应该可以完成工作而不会弄乱任何东西。
该代码需要对System.Windows.Forms
的引用。
InputLanguage origIpl = InputLanguage.CurrentInputLanguage;
Boolean fixIpl = false;
try
{
// This causes the exception.
CultureInfo ci = origIpl.Culture;
}
catch (ArgumentException)
{
// detected the problem.
fixIpl = true;
}
if (fixIpl)
{
InputLanguage validIpl = null;
InputLanguage preferredIpl = null;
foreach (InputLanguage ipl in InputLanguage.InstalledInputLanguages)
{
try
{
// This causes the exception.
CultureInfo ci = ipl.Culture;
// Store the first one that survives the above call
if (validIpl == null)
validIpl = ipl;
// See if there is one with identical layout. If not, keep looking.
if (origIpl.LayoutName != ipl.LayoutName)
continue;
// Found 1:1 match: store and abort loop.
preferredIpl = ipl;
break;
}
catch (ArgumentException)
{
/* skip languages that raise the exception here. */
}
}
if (validIpl != null)
InputLanguage.CurrentInputLanguage = preferredIpl ?? validIpl;
// else case here could give some kind of warning?
// Not sure if it's possible to NOT have any standard input languages.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.