当键盘出现在 Entry 控件 Xamarin Forms 中时,调整页面内容并将其稍微向上移动 [英] adjust and move the content of a page up slightly when the keyboard appears in an Entry control Xamarin Forms

查看:30
本文介绍了当键盘出现在 Entry 控件 Xamarin Forms 中时,调整页面内容并将其稍微向上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册表单,当用户输入诸如电子邮件之类的数据时,在该输入控件下方有一个标签,如果输入中有错误,则会出现该标签.所以我的问题是虚拟键盘隐藏了显示输入错误的标签,我不希望这种情况发生.

I have a registration form, when the user is entering data such as Email, below that Entry control there is a Label that appears if there is an error in the input. So my problem is that the virtual keyboard hides the Label showing input errors and I don't want that to happen.

使用 keyboard.jpg 没有keyboard.jpg

将有一些方法可以将表单的内容移高一点,以便可以看到控制条目和错误标签

It will be that there will be some way to move the content of the form a little higher so that the Control Entry can be seen along with the Error Label

<StackLayout>
<Entry
    Keyboard="Email"
    MaxLength="30"
    Placeholder="Enter Email"
    ReturnType="Next"
    Style="{StaticResource BorderlessEntryStyle}"
    Text="{Binding Email.Value}">
    <Entry.Behaviors>
        <behaviorsValidate:EventToCommandBehavior Command="{Binding ValidateEmailCommand}" EventName="TextChanged" />
    </Entry.Behaviors>
</Entry>
<Label
    Margin="4,-4,0,0"
    FontSize="12"
    IsVisible="{Binding Email.IsValid, Converter={StaticResource InverseBoolConverter}}"
    Style="{StaticResource SimpleLabelStyle}"
    Text="{Binding Email.Errors, Converter={StaticResource FirstValidationErrorConverter}}"
    TextColor="{DynamicResource Red}"
    VerticalOptions="FillAndExpand" />
</StackLayout>

推荐答案

关于在 Xamarin Forms 中显示键盘时调整元素,找到一种方法来做到这一点.

About adjusting elements when keyboard shows in Xamarin Forms, find one way to do this.

在 Android 上,您只需要在 Grid 中添加元素并在应用程序 XAML 中使用平台特定的 UseWindowSoftInputModeAdjust Resize.

On android you just need to add your elements inside a Grid and use the platform specific UseWindowSoftInputModeAdjust Resize in the Application XAML.

首先,在共享代码中创建一个从 Grid 扩展的新类.

firstly, create a new class that extend from Grid in Shared code.

 public class KeyboardView: Grid
{

}

然后在其中添加您的控件.

Then adding your control inside it.

<views:KeyboardView Padding="0,60,0,0"
                     VerticalOptions="FillAndExpand">
             <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="60" />
                <RowDefinition Height="50" />
                <RowDefinition Height="50" />
                <RowDefinition Height="*" />
              </Grid.RowDefinitions>
                
             <Image Source="ic_test"
                    HeightRequest="80"
                    WidthRequest="80"
                    HorizontalOptions="CenterAndExpand"
                    Grid.Row="0"/>
                
             <Label Text="Login"
                    FontAttributes="Bold"
                    TextColor="CornflowerBlue"
                    HorizontalOptions="CenterAndExpand"
                    FontSize="25"
                    VerticalOptions="Center"
                    Margin="0,20,0,0"
                    Grid.Row="1"
                    x:Name="welcomeText"/>
                
        
             <Entry Placeholder="Email"
                    Grid.Row="2"
                    Margin="20,0"                        
                    x:Name="email" 
                    ReturnType="Done"
                    Keyboard="Email"/>
    
             <Entry Placeholder="Password"
                    Margin="20,0" 
                    Grid.Row="3"
                    HeightRequest="50"
                    x:Name="password"                      
                    ReturnType="Done"
                    IsPassword="true"/>
                
            
            <Button VerticalOptions="EndAndExpand"
                    BackgroundColor="CornflowerBlue"
                    HeightRequest="60"
                    TextColor="White"
                    CornerRadius="0"
                    Grid.Row="4"
                    Text="Login"/>
        </views:KeyboardView>

第三,在应用程序 XAML 上添加特定于平台的 UseWindowSoftInputModeAdjust 和 Resize 值

Thirdly, add platform specific UseWindowSoftInputModeAdjust with Resize value on the Application XAML

<Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
         x:Class="KeyboardSample.App"
         xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
         android:Application.WindowSoftInputModeAdjust="Resize">

在 iOS 上,我们必须创建一个自定义渲染器来调整大小.不要在 ios 设备上测试.

On iOS we have to create a custom renderer to do the resize. Don't test on ios device.

[assembly: ExportRenderer(typeof(KeyboardView), typeof(KeyboardViewRenderer))]
namespace KeyboardSample.iOS.Renderers
{
public class KeyboardViewRenderer : ViewRenderer 
{
    NSObject _keyboardShowObserver;
    NSObject _keyboardHideObserver;
    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            RegisterForKeyboardNotifications();
        }

        if (e.OldElement != null)
        {
            UnregisterForKeyboardNotifications();
        }
    }

    void RegisterForKeyboardNotifications()
    {
        if (_keyboardShowObserver == null)
            _keyboardShowObserver = UIKeyboard.Notifications.ObserveWillShow(OnKeyboardShow);
        if (_keyboardHideObserver == null)
            _keyboardHideObserver = UIKeyboard.Notifications.ObserveWillHide(OnKeyboardHide);
    }

    void OnKeyboardShow(object sender, UIKeyboardEventArgs args)
    {

        NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
        CGSize keyboardSize = result.RectangleFValue.Size;
        if (Element != null)
        {
            Element.Margin = new Thickness(0, 0, 0, keyboardSize.Height); //push the entry up to keyboard height when keyboard is activated

        }
    }

    void OnKeyboardHide(object sender, UIKeyboardEventArgs args)
    {
        if (Element != null)
        {
            Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed
        }

    }


    void UnregisterForKeyboardNotifications()
    {
        if (_keyboardShowObserver != null)
        {
            _keyboardShowObserver.Dispose();
            _keyboardShowObserver = null;
        }

        if (_keyboardHideObserver != null)
        {
            _keyboardHideObserver.Dispose();
            _keyboardHideObserver = null;
        }
    }
}
 }

这篇关于当键盘出现在 Entry 控件 Xamarin Forms 中时,调整页面内容并将其稍微向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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