如何在xamarin.forms

时间:2015-08-06 13:45:00

标签: xamarin.forms

我有一个最顶层的网格

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

在我的页面中。 我怎么能有*行来调整它的高度以尊重屏幕键盘的存在与否? 因此,当键盘出现时,第0行中的内容会缩小。 或者,至少如何检测键盘显示 在编辑? 我已经为该编辑器提供了自定义渲染器 因此,可以快速完成特定平台特定代码的填充。

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

现在我已经改编了这里找到的解决方案 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);
        }
    }

共享平台&#34;独立&#34;代码:

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

public class ExtendedEditor : Editor ...
{