亚洲必赢手机入口WPF中Popup上的textbox无法切换至中文输入法。WPF中Popup上的textbox无法切换到中文输入法。

by admin on 2018年10月5日

As Marco Zhou has said in the msdn forum
(http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b2428b85-adc9-4a1e-a588-8dbb3b9aac06/):
Windows will only send WM_IME_SETCONTEXT message to the active window,
Popup by default is designed to be shown with WM_EX_NOACTIVE which
means that it doesn’t in active state when displaying, that’s why IME
could not work correctly in this regard, to workaround this issue, you
could try set the Popup as the active window using win32
SetActiveWindow() API.

As Marco Zhou has said in the msdn forum
(http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b2428b85-adc9-4a1e-a588-8dbb3b9aac06/):
Windows will only send WM_IME_SETCONTEXT message to the active window,
Popup by default is designed to be shown with WM_EX_NOACTIVE which
means that it doesn’t in active state when displaying, that’s why IME
could not work correctly in this regard, to workaround this issue, you
could try set the Popup as the active window using win32
SetActiveWindow() API.

So the workaround is use MyPopup instead of Popup:

So the workaround is use MyPopup instead of Popup:

public class MyPopup : Popup
{
    [DllImport(“user32.dll”)]
    static extern IntPtr SetActiveWindow(IntPtr hWnd);

public class MyPopup : Popup
{
    [DllImport(“user32.dll”)]
    static extern IntPtr SetActiveWindow(IntPtr hWnd);

    static MyPopup()
    {
        EventManager.RegisterClassHandler(
            typeof(MyPopup),
            Popup.PreviewGotKeyboardFocusEvent,
            new
KeyboardFocusChangedEventHandler(OnPreviewGotKeyboardFocus),
            true);
    }

    static MyPopup()
    {
        EventManager.RegisterClassHandler(
            typeof(MyPopup),
            Popup.PreviewGotKeyboardFocusEvent,
            new
KeyboardFocusChangedEventHandler(OnPreviewGotKeyboardFocus),
            true);
    }

    private static void OnPreviewGotKeyboardFocus(Object sender,
KeyboardFocusChangedEventArgs e)
    {
        var textBox = e.NewFocus as TextBoxBase;
        if (textBox != null)
        {
            var hwndSource = PresentationSource.FromVisual(textBox) as
HwndSource;
            if (hwndSource != null)
            {
                SetActiveWindow(hwndSource.Handle);
            }
        }
    }
}

    private static void OnPreviewGotKeyboardFocus(Object sender,
KeyboardFocusChangedEventArgs e)
    {
        var textBox = e.NewFocus as TextBoxBase;
        if (textBox != null)
        {
            var hwndSource = PresentationSource.FromVisual(textBox) as
HwndSource;
            if (hwndSource != null)
            {
                SetActiveWindow(hwndSource.Handle);
            }
        }
    }
}

官方解决方案

合法解决方案

采用的言辞自己盖一个接近代码如齐,控件内local:MyPopup
等同于Popup,亲测好只要,实用。

运的语句自己修一个看似代码如齐,控件内local:MyPopup
等同于Popup,亲测好要,实用。

参考:https://connect.microsoft.com/VisualStudio/feedback/details/389998/wpf-popup-messes-with-ime-switching

参考:https://connect.microsoft.com/VisualStudio/feedback/details/389998/wpf-popup-messes-with-ime-switching

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图