使用国际键盘时,Windows 10上的WPF应用程序崩溃

问题描述 投票:3回答:1

在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();
}
c# wpf windows-10 culture
1个回答
0
投票

我在我的程序调用的库中遇到了这个问题,没有任何摆弄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.
}
© www.soinside.com 2019 - 2024. All rights reserved.