【问题标题】:Xamarin.forms - Trouble behaviour between ListView & keyboardXamarin.forms - ListView 和键盘之间的故障行为
【发布时间】:2020-06-11 07:40:38
【问题描述】:

我正在处理 Xamarin.forms 应用程序聊天,但我在使用 listView 时遇到了一些麻烦。第一个是当我准备好输入一些信息时,键盘与主聊天重叠。第二个发生在我隐藏键盘并且listView 没有完全向下滚动时。这类问题有一些约定的解决方案吗?这是视频和相应的代码:

VIDEO

我的 xaml 页面:

  <StackLayout>


        <ListView 
                    x:Name="MessagesListView" 
                    ItemTemplate="{StaticResource MessageTemplateSelector}" 
                    ItemsSource="{Binding Pedidos}" 
                    HasUnevenRows="True"  SeparatorVisibility="None"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" IsEnabled="True" SelectionMode="None" BackgroundColor="{StaticResource BackGroundColor}"/>


        <StackLayout Orientation="Horizontal" BackgroundColor="{StaticResource white}" Grid.Row="1">

            <local1:CustomEntry 
                HorizontalOptions="FillAndExpand"  

                Placeholder="{translator:Translate Mensagem}"  
                Keyboard="Chat"  Margin="8,0,0,0" PlaceholderColor="{StaticResource PlaceHolderText}" TextColor="{StaticResource texto}"  FontSize="14" Text="{Binding Pedido.Message}" x:Name="Message"/>
            <Image Source="send.png" WidthRequest="32" HeightRequest="32" Margin="8,8,8,8" BackgroundColor="Transparent">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer
                           Command="{Binding EnviarPedidoCommand}"
                           Tapped="Button_Clicked"
                            />
                </Image.GestureRecognizers>
            </Image>
        </StackLayout>

</StackLayout>

后端页面:

 public ChatPage(Classes.ClassConversation.ResultadoConversation conversation, Classes.ClassUser.Result user)
    {
        InitializeComponent();

        GetResult = conversation;
        ConnectionsStrings.GlobalVar.email = user.rowKey;
        GetUser = user;

        Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);

        _viewModel = new ViewModels.ConnectionHub(conversation, user);

    }

    protected override async void OnAppearing()
    {

        base.OnAppearing();
        GetHistoryOfConversation(GetResult.rowKey, ConnectionsStrings.GlobalVar.Token, GetUser);

        BindingContext = _viewModel;

        await _viewModel.Connect();
    }

    protected override async void OnDisappearing()
    {
        base.OnDisappearing();
        await _viewModel.Disconnect();
    }

    //Limpa o campo mensagem quando envia mensagem
    private void Button_Clicked(object sender, EventArgs e)
    {
        Message.Text = "";     
    }

    //Função que recebe o historico de mensagens daquela conversa
    async void GetHistoryOfConversation(string conversationId, string token, Classes.ClassUser.Result user)
    {
        try
        {
            Classes.ClasMessage messages = await Servicos.Servicos.GetHistoryOfConversation(conversationId, token, user, GetResult);
            _viewModel.Pedidos = new ObservableRangeCollection<Classes.SendMessage>(messages.result);

            var target = _viewModel.Pedidos[_viewModel.Pedidos.Count - 1];
            MessagesListView.ScrollTo(target, ScrollToPosition.End, false);

            var result = await Servicos.Servicos.ReadConversation(conversationId, GetResult);
        }
        catch (Exception)
        {
            await DisplayAlert(Languages.AppResources.Notifications, Languages.AppResources.ErrorOccurred, "Ok");
        }
    }

【问题讨论】:

  • 这可能会对您有所帮助。 stackoverflow.com/questions/40373761/…。您必须将 ListView 滚动到最后一项。
  • 感谢您的回复,但它不起作用。它可以显示,但是当我通过视图模型插入注册时它不起作用。
  • 请尝试旋转列表视图并在列表顶部添加项目

标签: listview xamarin.forms xamarin.android xamarin.ios keyboard


【解决方案1】:

第一个是当我准备好输入一些信息时, 键盘与主聊天重叠。

对于第一个问题,您应该检测键盘向上/向下并向上/向下移动 listView。

Android中,您可以使用Soft Keyboard Input Mode

<Application ...
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
             android:Application.WindowSoftInputModeAdjust="Resize">
  ...
</Application>

iOS 中,您需要编写自定义渲染器来检测事件。

您可以查看this thread 中的答案并阅读this blog 以获取解决方案。

第二个发生在我隐藏键盘和 listView 时 不会一直向下滚动。

将消息插入 listView 的 itemSource 后,您应该滚动 EnviarPedidoCommand 中的 listView。

【讨论】:

  • 你好,我在android上已经使用“resize”了,问题是当键盘打开时listview的内容,listview的内容没有显示最后一条消息。要查看此消息,您需要滚动。如果我使用“平移”,则不会出现上面的问题,但工具栏会消失,并且列表视图滚动不会显示所有数据。
  • 你在 iOS 上有什么问题?
  • 在 ios 上运行正常。当键盘打开时,会显示列表视图中的最后一项。
  • 您是否在发送消息后将 listView 滚动到 EnviarPedidoCommand 中的最后一个元素?您应该使用“调整大小”。
  • 。我对 SendRequestCommand 的问题是该命令在视图模型中。然后我无法访问列表滚动。我正在使用调整大小。你可以在我的代码中看到。
猜你喜欢
  • 1970-01-01
  • 2014-07-02
  • 2020-04-10
  • 2014-12-18
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
相关资源
最近更新 更多