c# - Xamarin.iOS CollectionView - 添加新元素会导致整个列表被刷新
问题描述
我正在构建一个 xamarin.forms 聊天应用程序,并且只需要添加交付的新元素,在 Android 上我有预期的行为,但 iOS 会刷新所有导致糟糕的用户体验的 CollectionView。请查看我的代码(仅相关部分),让我知道我做错了什么,或者我错过了什么在 iOS 上也有这种行为。
我的 .xaml:
<CollectionView x:Name="ChatCollectionView"
SelectionMode="None" HorizontalScrollBarVisibility="Never"
VerticalScrollBarVisibility="Always" ItemsUpdatingScrollMode="KeepLastItemInView"
ItemTemplate="{StaticResource MsgTemplateSelector}" Margin="5,0,5,10">
</CollectionView>
.cs 文件:
public static ReaderWriterLockSlim valuesLock = new ReaderWriterLockSlim();
void ObservableCollectionCallback(IEnumerable collection, object context, Action accessMethod, bool writeAccess)
{
var collectionLock = (ReaderWriterLockSlim)context;
Action enterLock = writeAccess ? new Action(collectionLock.EnterWriteLock) : new Action(collectionLock.EnterReadLock);
Action exitLock = writeAccess ? new Action(collectionLock.ExitWriteLock) : new Action(collectionLock.ExitReadLock);
enterLock();
try { accessMethod(); } finally { exitLock(); }
}
ObservableCollection<Msg> ListMsg;
public Page(string userid, string chatkey, string titulo, string foto)
{
// some code ...
this.BindingContext = this;
BindingBase.EnableCollectionSynchronization(ListMsg as IEnumerable, valuesLock,ObservableCollectionCallback);
List<Msg> recipient = await Task.Run(()=> client.PostData<List<Msg>>("url/msg.php"));
//previous line returns a json and deserialize it to a list of "Msg"
if (recipient != null)
{
ListMsg = new ObservableCollection<Msg>(recipient);
ChatCollectionView.ItemsSource = ListMsg;
if (ListMsg.Count > 0)
{
if(Device.RuntimePlatform==Device.iOS)
await Task.Delay(2000);
ChatCollectionView.ScrollTo(ListMsg.Count - 1, position: ScrollToPosition.End, animate: false);
}
}
else
ChatCollectionView.ItemsSource = new ObservableCollection<Msg>();
MessagingCenter.Subscribe<MainPage, Msg>("Page", "scroll", async (sender, arg) =>
{
Task.WaitAll(tasks.ToArray());
tasks.Add(Task.Run(async () =>
{
arg = await MsgPrepare(arg); //this only changes the element arg not the list
_ = Device.InvokeOnMainThreadAsync(() =>
{
valuesLock.EnterWriteLock();
try { ListMsg.Add(arg); }
finally { valuesLock.ExitWriteLock(); }
});
}));
}
}
解决方案
通过“错误”修复它。我添加了涉及整个页面以修复 iOS 软键盘(覆盖了焦点条目元素)和宾果游戏的问题,它已经修复了这两个问题。如果我取下 ScrollView,问题又回来了。我还注意到,如果 ScrollView 仅包含 CollectionView,它就可以工作,它不需要像我的情况那样包含整个页面。另一点是如果你不想要 ScrollView 的滚动功能,你可以设置 Orientation=Neither 来避免它。希望这对你们有帮助。
推荐阅读
- javascript - 对整个 csv 列求和以使用 d3 javascript 显示总数
- angular - 我必须登录才能查看该页面。Angular 9 通用 + Firebase
- markdown - 如何在降价中禁用标题的粗体?
- parquet - Azure 数据工厂中的 Parquet 文件名
- java - Java 软引用优先级
- c - 是否可以仅使用不带条件变量的互斥体来实现生产者消费者?
- mysql - 使用触发器确保插入的数据不超过限制
- php - 将 SQL 文件导入 XAMPP 错误。可能的代码错误?
- flutter - 热重载时有效,但重新安装时无效
- angular - WARNING in Exceeded maximum budget for SCSS FILE IN ANGULAR