xamarin Forms:根据键盘高度向上移动视图 [英] xamarin Forms: Move the view Upward according to keyboard height

查看:19
本文介绍了xamarin Forms:根据键盘高度向上移动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 xamarin 表单.我在 xaml 中设计了一个登录表单页面.我想在键盘出现时向上移动登录表单视图,以便在平台 AndroidIOS 的情况下都可以看到文本字段和登录按钮.如何计算键盘高度并通过动态计算键盘高度将Login表单视图向上移动.

I am using xamarin forms. I have designed a login form page in xaml. I want to shift the login form view upward when the keyboard appears, so that both the text field and login button is visible in case of both the platform Android and IOS. How to calculate the keyboard height and shift the Login form view upward by dynamically calculating the keyboard height.

下面是我的 xaml 代码:

<ContentPage>
    <ScrollView>
        <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Grid Padding="20, 30, 20, 20" RowSpacing="20" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Image Grid.Row="0" Source="login.png" HorizontalOptions="Center" VerticalOptions="Center"/>

                <Entry Grid.Row="2" x:Name="entryUserName" HorizontalOptions="Fill"  Placeholder="Username" PlaceholderColor="#707070" Text="{Binding UserName,Mode=TwoWay}" Margin="5"/>

                <BoxView Grid.Row="2" HeightRequest="1" HorizontalOptions="Fill" BackgroundColor="#707070" VerticalOptions="End"/>

                <Entry Grid.Row="3" x:Name="entryPassword" Text="{Binding Password,Mode=TwoWay}" Placeholder="Password" PlaceholderColor="#707070" Margin="5" HorizontalOptions="Fill" IsPassword="True"/>

                <BoxView Grid.Row="3" HeightRequest="1" HorizontalOptions="Fill" BackgroundColor="#707070" VerticalOptions="End"/>

                <Button Grid.Row="5" HorizontalOptions="Center" VerticalOptions="Center" Text="Login" Command="{Binding doLoginCommand}" CommandParameter="entryUserName,entryPassword" />

            </Grid>         
        </AbsoluteLayout>
    </ScrollView>
</ContentPage>

我不想对页面执行任何类型的自定义呈现.有没有什么资源可以让我编写依赖服务来计算不同移动视图跨平台的键盘高度.我经历了这个,但它有某种我没有的自定义渲染不想.

I don't want to perform any kind of custom rendering of the page. Is there any resource through which I can write the dependency service to calculate the keyboard height of different mobile view cross platform. I gone through this but it has some kind of custom rendering which I don't want.

推荐答案

选项 1:

安卓:将此添加到您的清单中:

Android: Add this to your manifest:

<activity //Your MainActivity
        android:windowSoftInputMode="stateVisible|adjustResize" ... >
        ...
</activity>

IOS:添加这个 nuget 包:https://www.nuget.org/packages/Xam.Plugins.Forms.键盘重叠/

Ios: Add this nuget package: https://www.nuget.org/packages/Xam.Plugins.Forms.KeyboardOverlap/

并初始化它:

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

选项 2:

将此代码添加到您的 Page 类:

Add this code to your Page class:

        protected override void OnAppearing()
        {
            base.OnAppearing();
            entryUserName.Focused += InputFocused;
            entryPassword.Focused += InputFocused;
            entryUserName.Unfocused += InputUnfocused;
            entryPassword.Unfocused += InputUnfocused;
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            entryUserName.Focused -= InputFocused;
            entryPassword.Focused -= InputFocused;
            entryUserName.Unfocused -= InputUnfocused;
            entryPassword.Unfocused -= InputUnfocused;
        }
        void InputFocused(object sender, EventArgs args){
            Content.LayoutTo(new Rectangle(0,-360, Content.Bounds.Width, Content.Bounds.Height));
        }

        void InputUnfocused(object sender, EventArgs args){
            Content.LayoutTo(new Rectangle(0,0, Content.Bounds.Width, Content.Bounds.Height));
        }

这篇关于xamarin Forms:根据键盘高度向上移动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆