显示键盘时启用页面滚动

时间:2017-09-26 11:58:53

标签: xamarin xamarin.forms keyboard scrollview

我的页面如下:

enter image description here

如果用户关注其中一个条目,则页面被锁定"。用户无法上下移动:

enter image description here

我已将ContentPage与ScrollView一起用作主要布局。 我还尝试在各种模式下设置Window.SetSoftInputMode(),但一切都保持不变。

有没有任何模块化方法来解决这个问题(我在HeightRequest = 0的条目上面有一个StackLayout的解决方法,当其中一个条目被聚焦时,我将HeightRequest更改为键盘的高度)?

更新 我实现了它如下:
添加了新的自定义控件CustomEntry,基本上是Entry:

    <?xml version="1.0" encoding="UTF-8"?>
    <Entry xmlns="http://xamarin.com/schemas/2014/forms"
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           x:Class="MyApp.Framework.Controls.CustomEntry"
           TextColor="{StaticResource MainPurple}"
           PlaceholderColor="{StaticResource MainPurpleLight}"
           HeightRequest="45"
           FontSize="14"
           Focused="CustomEntryFocused"
           Unfocused="CustomEntryUnfocused">    
    </Entry>

private void CustomEntryFocused(object sender, FocusEventArgs e)
        {
            var stackParent = StackParent as StackLayout;
            stackParent?.Children.Add(new StackLayout() { HeightRequest = `KeyboardHeight });`
        }

private void CustomEntryUnfocused(object sender, FocusEventArgs e)
{
    var stackParent = StackParent as StackLayout;               
    stackParent?.Children.RemoveAt(stackParent.Children.Count - 1);
}

3 个答案:

答案 0 :(得分:0)

仅在iOS项目中使用Xam.Plugins.Forms.KeyboardOverlap插件(不是PCL,而不是Android),以及iOS项目调用:

Xamarin.Forms.Init();//platform specific init
KeyboardOverlapRenderer.Init ();

您必须在致电Xamarin.Forms.Init().

后执行此操作

enter image description here

以下是一个示例:https://github.com/paulpatarinski/Xamarin.Forms.Plugins/tree/master/KeyboardOverlap/SampleApp

答案 1 :(得分:0)

您也可以使用以下代码手动滚动页面

void EntryKeyboardHandle_Focused(object sender, Xamarin.Forms.FocusEventArgs e)
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                var height = SignupGrid.Height;
                await MainScroll.ScrollToAsync(0, height, true);
            });
        }

您需要将网格或stacklayout放在滚动视图中,并将焦点事件放在条目上。

Focused="EntryKeyboardHandle_Focused"

答案 2 :(得分:0)

更新:添加了新的自定义控件CustomEntry,它基本上是具有扩展功能的默认条目:

<?xml version="1.0" encoding="UTF-8"?>
<Entry xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       x:Class="MyApp.Framework.Controls.CustomEntry"
       TextColor="{StaticResource MainPurple}"
       PlaceholderColor="{StaticResource MainPurpleLight}"
       HeightRequest="45"
       FontSize="14"
       Focused="CustomEntryFocused"
       Unfocused="CustomEntryUnfocused">    
</Entry>

然后在此条目上设置Focus和Unfocus方法:

private void CustomEntryFocused(object sender, FocusEventArgs e)
{
    var stackParent = StackParent as StackLayout;
    stackParent?.Children.Add(new StackLayout() { HeightRequest = `KeyboardHeight });
}

private void CustomEntryUnfocused(object sender, FocusEventArgs e)
{
    var stackParent = StackParent as StackLayout;               
    stackParent?.Children.RemoveAt(stackParent.Children.Count - 1);
}