收听软键盘的显示/隐藏事件(Windows 8.1 RT,XAML)

时间:2015-02-25 22:08:38

标签: c# wpf xaml windows-phone-8 windows-phone-8.1

如何在我的应用程序或页面中显示/关闭软键盘?

THIS文章中找到此snipplet。但对WP 8.1 RT无效。我该如何翻译或获得类似的行为?

public class MyApplication
{
    public MyApplication()
    {
        // Grab the input pane for the main application window and attach
        // touch keyboard event handlers.
        Windows.Foundation.Application.InputPane.GetForCurrentView().Showing
            += new EventHandler(_OnInputPaneShowing);
        Windows.Foundation.Application.InputPane.GetForCurrentView().Hiding
            += new EventHandler(_OnInputPaneHiding);
    }

    private void _OnInputPaneShowing(object sender, IInputPaneVisibilityEventArgs eventArgs)
    {
        // If the size of this window is going to be too small, the app uses 
        // the Showing event to begin some element removal animations.
        if (eventArgs.OccludedRect.Top < 400)
        {
            _StartElementRemovalAnimations();

            // Don't use framework scroll- or visibility-related 
            // animations that might conflict with the app's logic.
            eventArgs.EnsuredFocusedElementInView = true;
        }
    }

    private void _OnInputPaneHiding(object sender, IInputPaneVisibilityEventArgs eventArgs)
    {
        if (_ResetToDefaultElements())
        {
            eventArgs.EnsuredFocusedElementInView = true;
        }
    }

    private void _StartElementRemovalAnimations()
    {
        // This function starts the process of removing elements 
        // and starting the animation.
    }

    private void _ResetToDefaultElements()
    {
        // This function resets the window's elements to their default state.
    }
}

2 个答案:

答案 0 :(得分:3)

您引用的文章适用于Windows运行时应用,但它有一个小错误。 InputPane位于Windows.UI.ViewManagement,不在Windows.Foundation.Application中。改变它,其余的应该可以正常工作。

我已经报告了文档错误,因此可以修复。

答案 1 :(得分:3)

找到答案,注册这些事件就足够了:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
        InputPane.GetForCurrentView().Showing += onKeyboardShowing;
        InputPane.GetForCurrentView().Hiding += onKeyboardHidding;
}

private void onKeyboardShowing(InputPane sender, InputPaneVisibilityEventArgs args)
{
    KeyboardVisible = true;
}

private void onKeyboardHidding(InputPane sender, InputPaneVisibilityEventArgs args)
{

    KeyboardVisible = false;
}