如何在 xamarin.forms 中处理键盘 appearance/disappearance

How to I handle keyboard appearance/disappearance in xamarin.forms

我有一个最上面的网格

<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="120"/>
<RowDefinition Height="1"/>
<RowDefinition Height="5"/>
<RowDefinition Height="35"/>
<RowDefinition Height="5"/>
</Grid.RowDefinitions>

在我的页面中。 我怎么能有 * 行来调整它的高度以尊重屏幕键盘的存在或不存在? 使第 0 行的内容随着键盘的出现而缩小。 或者,至少我怎么能检测到键盘出现 在编辑器上? 我已经为该编辑器创建了一个自定义渲染器 因此可以快速填充额外的平台特定代码。

您只能手动控制 Grid 行大小。 Editor.FocusedEditor.Unfocused 就是您要查找的内容。

但您可以将其与事件触发器结合使用(http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/triggers/):

<EventTrigger Event="Focused">
    <local:FocusedTriggerAction />
</EventTrigger>
public class FocusedTriggerAction : TriggerAction<Editor>
{
    protected override void Invoke (Editor editor)
    {
        yourRow.Height = new GridLength(100);
    }
}

目前我已经采用了此处找到的解决方案 http://www.gooorack.com/2013/08/28/xamarin-moving-the-view-on-keyboard-show/

    private UIView activeview;             // Controller that activated the keyboard

    /// <summary>
    /// Initializes a new instance of the <see cref="ExtendedEditorRenderer"/> class.
    /// </summary>
    public ExtendedEditorRenderer ()
    {
        NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification,KeyBoardUpNotification);
        NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification,KeyBoardDownNotification);
    }

    public void Dispose()
    {
        NSNotificationCenter.DefaultCenter.RemoveObserver(UIKeyboard.DidShowNotification);
        NSNotificationCenter.DefaultCenter.RemoveObserver(UIKeyboard.WillHideNotification);
        base.Dispose();
    }
    private void KeyBoardDownNotification(NSNotification notification)
    {
        try
        {
            var view = (ExtendedEditor)Element;
            if (view.KeyboardListener != null)
            {
                Size s = new Size(0, 0);
                view.KeyboardListener.keyboardSizeChangedTo(s);
            }
        }
        catch (Exception ex)
        {
            //Debug.WriteLine("dcaught {0}", ex);
        }
    }

    private void KeyBoardUpNotification(NSNotification notification)
    {
        try
        {
            // get the keyboard size
            CGRect r = UIKeyboard.BoundsFromNotification(notification);
            Size s = new Size(r.Size.Width, r.Size.Height);
            var v = (ExtendedEditor)Element;
            v.KeyboardListener.keyboardSizeChangedTo(s);
        }
        catch(Exception ex) {
            //Debug.WriteLine("scaught {0}", ex);
        }
    }

共享平台"independent"代码:

public interface IKeyboardListener
{
    void keyboardSizeChangedTo(Size s);
}

public class ExtendedEditor : Editor ...
{