如何防止编辑器在Xamarin.Forms中隐藏在键盘后面?

如何防止编辑器在Xamarin.Forms中隐藏在键盘后面?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一个聊天应用程序。目前,有一个用于添加聊天文本的入口控件。现在我想提供多行输入,就像Whatsapp一样 如果用户键入多行,则应自动将文本换行到下一行 若用户点击移动键盘上的“下一行”按钮,它应该转到下一行 输入的高度应该自动增加到3行,如果用户删除文本,它也应该减少 为此,我尝试用编辑器替换条目,并实现以下功能 1-将编辑器放置在条目的位置。 2-实现一种功能,在用户单击消息列表屏幕或后退按钮之前保持键盘打开 现在我正在尝试实现自动高度功能,但当用户尝试键入时,编辑器会在键盘后面。有谁

我有一个聊天应用程序。目前,有一个用于添加聊天文本的入口控件。现在我想提供多行输入,就像Whatsapp一样

  • 如果用户键入多行,则应自动将文本换行到下一行
  • 若用户点击移动键盘上的“下一行”按钮,它应该转到下一行
  • 输入的高度应该自动增加到3行,如果用户删除文本,它也应该减少
为此,我尝试用编辑器替换条目,并实现以下功能

1-将编辑器放置在条目的位置。 2-实现一种功能,在用户单击消息列表屏幕或后退按钮之前保持键盘打开

现在我正在尝试实现自动高度功能,但当用户尝试键入时,编辑器会在键盘后面。有谁能建议我如何保持编辑器的开放性和自动调整大小吗

当前代码:

XAML:

编辑器渲染器:
公共类ChatEditorRenderer:EditorRenderer
{
受保护的覆盖无效OneElementPropertyChanged(对象发送方,System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
}
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null){
Control.ScrollEnabled=false;
}
var element=此.element作为ChatEditorWithPlaceholder;
Control.InputAccessoryView=null;
Control.ShouldEndEditing+=禁用隐藏键盘;
MessagingCenter.Subscribe(此为“FocusKeyboardStatus”,(发件人)=>
{
if(控件!=null)
{
Control.ShouldEndEditing+=启用隐藏键盘;
}
取消订阅(此为“FocusKeyboardStatus”);
});
}
专用布尔禁用隐藏键盘(UITextView textView)
{
返回false;
}
专用布尔启用隐藏键盘(UITextView textView)
{
返回true;
}
截图:

尝试此ios渲染器

using System;  
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;   

[assembly:ExportRenderer( typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace YourNameSpace.iOS
{
    public class CustomEditorRenderer: EditorRenderer
    {
        public ChatEntryRenderer()
        {   
            UIKeyboard.Notifications.ObserveWillShow ((sender, args) => {

                if (Element != null)
                {
                    Element.Margin = new Thickness(0,0,0, args.FrameEnd.Height); //push the entry up to keyboard height when keyboard is activated
                }
            });

            UIKeyboard.Notifications.ObserveWillHide ((sender, args) => {

                if (Element != null)
                {
                       Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed
                }

            }); 
        }
    }
}
对于android,将此添加到MainActivity中

 App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().
    UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
App.Current.On()。
使用WindowsOfInputModeAdjust(WindowsOfInputModeAdjust.Resize);

对于Ios,只有一个插件。你可以用这个。链接是

对于ANDROID,您只需在加载应用程序(new App())方法后的MainActivity中设置以下代码


您可以尝试更改此行:

<local:ChatEditorWithPlaceholder  x:Name="txtMessage" Grid.Column="0"  TextChanged="EnableSend" Text="{Binding OutGoingText}"/>

对于这一个:

<Editor x:Name="txtMessage" Grid.Column="0" AutoSize="TextChanges" TextChanged="EnableSend" Text="{Binding OutGoingText}"/>


我正在检查这个。我会让你知道结果很快。我已经测试,它在一些情况下有效,但在其他情况下有问题。首先,我需要删除
Xam.Plugins.Forms.KeyboardOverlap
以使用您的解决方案,否则它将使用键盘空间+编辑器底部边距(与键盘相同)。一旦我删除它并使用您的解决方案,它就可以工作了。但还有另一个问题。编辑器和键盘显示正确,但消息(ListView)返回,并在屏幕上显示第3/4条消息。请指导。我想你需要手动将listview翻译成上面的条目焦点。查看此链接,谢谢。根据该答案中的建议,我已经尝试过,但
ScrollView
会产生问题。如果我使用
ScrollView
,该解决方案会有所帮助。在我的聊天页面中,我有一个2行的网格。第一行有ListView,第二行有带发送按钮的编辑器。如果我实现了
滚动视图
,则在加载时,编辑器和发送按钮会显示在屏幕下方。我每次都要把它拉上来。请注意。不要添加滚动视图。当editer关注时,我谈到将总布局移到键盘上方。你看到Srusti Thakkar的答案了吗?插件有以下问题:。这是Git bug报告:你试过这个吗?试过并为Textbox工作过。但是我有一个编辑器,可以使用
InvalidateMeasure
自动调整大小。所以,我有一个问题:。请给我建议。嘿!请尝试此渲染器一次。还有另一个问题。编辑器和键盘显示正确,但消息(ListView)返回,并在屏幕上显示第3/4条消息。请导游。此外,您还可以查看此问题的另一个答案。我以前也用同样的方法处理过。编辑器控件支持无自定义渲染器的占位符。此外,它还具有自动调整大小的属性,当用户键入多行时,该属性可自动实现多行。
 App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().
    UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
App.Current.On<Xamarin.Forms.PlatformConfiguration.Android().
UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using HBIClientFacingApp;
using HBIClientFacingApp.iOS;

[assembly:ExportRenderer( typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace YourNameSpace.iOS
{
    public class CustomEditorRenderer: EditorRenderer
    {
        public ChatEntryRenderer()
        {   
            UIKeyboard.Notifications.ObserveWillShow ((sender, args) => {

                if (Element != null)
                {
                    Element.Margin = new Thickness(0,0,0, args.FrameEnd.Height); //push the entry up to keyboard height when keyboard is activated
                }
            });

            UIKeyboard.Notifications.ObserveWillHide ((sender, args) => {

                if (Element != null)
                {
                       Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed
                }

            }); 
        }
    }
}
<local:ChatEditorWithPlaceholder  x:Name="txtMessage" Grid.Column="0"  TextChanged="EnableSend" Text="{Binding OutGoingText}"/>
<Editor x:Name="txtMessage" Grid.Column="0" AutoSize="TextChanges" TextChanged="EnableSend" Text="{Binding OutGoingText}"/>