public static class PageExtensions
{
private static readonly IDictionary<Type, WindowSoftInputModeAdjust> OriginalWindowSoftInputModeAdjusts = new Dictionary<Type, WindowSoftInputModeAdjust>();
public static void UseWindowSoftInputModeAdjust(this Page page, WindowSoftInputModeAdjust windowSoftInputModeAdjust)
{
var platformElementConfiguration = Xamarin.Forms.Application.Current.On<Android>();
var pageType = page.GetType();
if (!OriginalWindowSoftInputModeAdjusts.ContainsKey(pageType))
{
var originalWindowSoftInputModeAdjust = platformElementConfiguration.GetWindowSoftInputModeAdjust();
OriginalWindowSoftInputModeAdjusts.Add(pageType, originalWindowSoftInputModeAdjust);
}
platformElementConfiguration.UseWindowSoftInputModeAdjust(windowSoftInputModeAdjust);
}
public static void ResetWindowSoftInputModeAdjust(this Page page)
{
var pageType = page.GetType();
if (OriginalWindowSoftInputModeAdjusts.TryGetValue(pageType, out var originalWindowSoftInputModeAdjust))
{
OriginalWindowSoftInputModeAdjusts.Remove(pageType);
var platformElementConfiguration = Xamarin.Forms.Application.Current.On<Android>();
platformElementConfiguration.UseWindowSoftInputModeAdjust(originalWindowSoftInputModeAdjust);
}
}
}
以下是如何在页面中应用它:
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
this.UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
this.ResetWindowSoftInputModeAdjust();
}
}
2条答案
按热度按时间nkhmeac61#
我已经解决了我的问题。我所做的是在我的
page.xaml.cs
上实现这将在页面显示时显示键盘时调整窗口的大小,如果您希望保留
Entry
或其他元素的正常行为,请使用以下代码:WindowSoftInputModeAdjust.Pan
是Android在显示键盘时的默认行为。这样当页面消失时,设置将恢复为默认值。6l7fqoea2#
作为对@jbtamares解决方案的补充,我创建了一个扩展方法,它允许在受影响的页面消失后轻松地恢复到原来的调整大小模式。
一旦在页面上呼叫UseWindowSoftInputModeAdjust,扩充方法就会追踪原始的调整大小模式。ResetWindowSoftInputModeAdjust会读取原始的调整大小模式,并进行相应的设定。
这是扩展方法的代码:
以下是如何在页面中应用它:
希望这能有所帮助。如果你发现上面贴出的代码有任何问题,请告诉我。