显示键盘时在页面上启用滚动
Enable scroll on page when keyboard is displayed
我的页面是这样的:
如果用户关注其中一个条目,页面为 "locked"。用户不能上下移动如下:
我使用了带有 ScrollView 的 ContentPage 作为主要布局。
我也尝试在各种模式下设置 Window.SetSoftInputMode(),但一切都保持不变。
有没有任何模块化的方法来解决这个问题(我在 HeightRequest=0 的条目上方使用 StackLayout 的解决方法,当其中一个条目被聚焦时,我将 HeightRequest 更改为键盘的高度)?
仅在您的 iOS 项目中使用 Xam.Plugins.Forms.KeyboardOverlap 插件(不是 PCL,不是 Android),并且您 iOS 项目调用:
Xamarin.Forms.Init();//platform specific init
KeyboardOverlapRenderer.Init ();
您必须在调用 Xamarin.Forms.Init().
之后执行此操作
这里有一个例子:https://github.com/paulpatarinski/Xamarin.Forms.Plugins/tree/master/KeyboardOverlap/SampleApp
您也可以使用以下代码手动滚动页面
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"
更新:添加了新的自定义控件 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);
}
我的页面是这样的:
如果用户关注其中一个条目,页面为 "locked"。用户不能上下移动如下:
我使用了带有 ScrollView 的 ContentPage 作为主要布局。 我也尝试在各种模式下设置 Window.SetSoftInputMode(),但一切都保持不变。
有没有任何模块化的方法来解决这个问题(我在 HeightRequest=0 的条目上方使用 StackLayout 的解决方法,当其中一个条目被聚焦时,我将 HeightRequest 更改为键盘的高度)?
仅在您的 iOS 项目中使用 Xam.Plugins.Forms.KeyboardOverlap 插件(不是 PCL,不是 Android),并且您 iOS 项目调用:
Xamarin.Forms.Init();//platform specific init
KeyboardOverlapRenderer.Init ();
您必须在调用 Xamarin.Forms.Init().
这里有一个例子:https://github.com/paulpatarinski/Xamarin.Forms.Plugins/tree/master/KeyboardOverlap/SampleApp
您也可以使用以下代码手动滚动页面
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"
更新:添加了新的自定义控件 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);
}