【问题标题】:Enable scroll on page when keyboard is displayed显示键盘时启用页面滚动
【发布时间】:2019-06-28 17:15:15
【问题描述】:

我的页面如下所示:

如果用户关注其中一个条目,则页面被“锁定”。用户不能上下移动,如下所示:

我使用 ContentPage 和 ScrollView 作为主要布局。 我也尝试在各种模式下设置 Window.SetSoftInputMode(),但一切都保持不变。

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

【问题讨论】:

  • 你找到解决这个问题的方法了吗?
  • 是的,针对我的具体问题。检查下面的答案。

标签: xamarin xamarin.forms keyboard scrollview


【解决方案1】:

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

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

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

Focused="EntryKeyboardHandle_Focused"

【讨论】:

  • 这不是模块化的,每次使用此条目时我都必须更改代码,但是您告诉了我如何在 Forms 项目中实现这一点,而无需在自定义渲染器中添加任何代码。谢谢!
【解决方案2】:

仅在您的 iOS 项目(不是 PCL,不是 Android)中使用 Xam.Plugins.Forms.KeyboardOverlap 插件,并且您的 iOS 项目调用:

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

您必须在致电Xamarin.Forms.Init(). 之后执行此操作

这里是一个例子:https://github.com/paulpatarinski/Xamarin.Forms.Plugins/tree/master/KeyboardOverlap/SampleApp

【讨论】:

  • 我一直在寻找一个模块化的解决方案,通常在渲染器和原生项目中没有/或只有最少的代码。我将用我的解决方案更新这篇文章。谢谢!
【解决方案3】:

更新:添加了新的自定义控件 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多