【发布时间】:2020-06-11 07:40:38
【问题描述】:
我正在处理 Xamarin.forms 应用程序聊天,但我在使用 listView 时遇到了一些麻烦。第一个是当我准备好输入一些信息时,键盘与主聊天重叠。第二个发生在我隐藏键盘并且listView 没有完全向下滚动时。这类问题有一些约定的解决方案吗?这是视频和相应的代码:
我的 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