Xamarin:获取键盘高度跨平台

本文关键字:高度 跨平台 键盘 获取 Xamarin | 更新日期: 2023-09-27 18:08:25

我希望编写一个依赖项服务,以便在点击任何输入单元格后键盘出现时获取键盘高度。

PCL中的接口

public interface IKeyboardService
    {
        int GetHeight();
    }

Xaml代码
 <ContentPage.Content> 
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <StackLayout Grid.Row="1">
                <Entry Text="Tap to open the keyboard" Focused="entryUserName_Focused" />
                <Button Text="Submit" Clicked="Handle_Clicked" />
            </StackLayout>
        </Grid>     
    </ContentPage.Content>
代码背后的

   private void entryUserName_Focused(object sender, FocusEventArgs e)
        {
            int height= DependencyService.Get<IKeyboardService>().GetHeight();
        }

Android的重写接口

   public class Keyboard_Droid : IKeyboardService
    {
      public int GetHeight()
        {
            // Logic to implement
            return h;
        }
    }

IOS中的重写接口

public class Keyboard_IOS : IKeyboardService
        {
          public int GetHeight()
            {
                // Logic to implement
                return h;
            }
        }

谁能提出实现这个的建议?

Xamarin:获取键盘高度跨平台

Xamarin Forms Code Behind file如果您检查MainPage的高度,那么在触发了focused之后,它将立即返回设备高度。然而,我增加了一点延迟,并且能够在屏幕键盘显示后看到屏幕的高度。

private async void entryUserName_Focused(object sender, FocusEventArgs e)
{
    double fullHeight = Application.Current.MainPage.Height; 
    //Note: adjust 350ms if height results are inconsistent.
    await Task.Delay(350); 
    double availableScreenHeight = fullHeight - Application.Current.MainPage.Height;
}