在我的 Xamarin 窗体应用中,我有一个聊天页面,其中包含列出聊天项的列表视图。
这是聊天页面视图模型:
public class IndexViewModel : BaseViewModel
{
public ObservableCollection<Model.Chat> Items { get; set; }
public Command LoadItemsCommand { get; set; }
public IndexViewModel()
{
Items = new ObservableCollection<Model.Chat>();
LoadItemsCommand = new Command( () => ReadTheList());
MessagingCenter.Subscribe<RootPage, Model.Chat>(this, "NewMessage", (obj, item) =>
{
var chat = item as Model.Chat; // Inspecting this as a local variable during debugging looks fine
// when an item is added like this the app freezes
Items.Add(chat);
});
}
void ReadTheList()
{
var items = repo.ChatList();
Items.Clear();
foreach (var item in items)
{
// when an item is added like this, all is fine
Items.Add(item);
}
}
}
每当有新的聊天消息到达时,我都想向下滚动到最后一条消息(新添加的消息(。所以我在我的视图中添加了这个委托:
viewModel.Items.CollectionChanged += (sender, e) =>
{
if (viewModel.Items.Count == 0) return;
Debug.WriteLine("I can always see this when a chat item is added to the collection");
chatList.ScrollTo(viewModel.Items.Last(), ScrollToPosition.End, false); // this is where the freeze happens
};
当我从本地存储添加项目列表并且列表滚动到底部时,委托的最后一行工作正常,但是当通过消息中心订阅添加新收到的聊天项目时,它会冻结我的应用程序。当新消息到达并且页面打开并且我没有与页面上的任何内容交互时,这种情况会一直发生。
谁能帮我调试这个?
在处理繁重的后台进程(如处理列表搜索(时,请在单独的线程上进行以避免此类问题。
将CllectionChanged
处理程序更改为:
viewModel.Items.CollectionChanged += (sender, e) =>
{
if (viewModel.Items.Count == 0) return;
Debug.WriteLine("I can always see this when a chat item is added to the collection");
Device.BeginInvokeOnMainThread(() =>
chatList.ScrollTo(viewModel.Items.Last(), ScrollToPosition.End, false));
};