프로그래밍/App 개발
[Xamarin] NSInternalInconsistencyException
엽기토기
2020. 11. 26. 17:55
반응형
github.com/xamarin/Xamarin.Forms/issues/6920
[Bug] NSInternalInconsistencyException exception on iOS with CollectionView · Issue #6920 · xamarin/Xamarin.Forms
Description I have a Xamarin.Forms project that has a CollectionView that is bound to an ObservableCollection. If I call Clear() on the ObservableCollection and then call Add() to add an item I get...
github.com
CollectionView의 ItemsSource 에 ObservableCollection 을 대입해서 사용하면 (바인딩 X), UI쓰레드가 아닌 쓰레드에서 .Add 등의 동작을 하면 Exception 발생.
따라서,바인딩 통해 해결하였다.
View
BindingContext = PickerViewModel;
_myCollectionView.SetBinding(ItemsView.ItemsSourceProperty, "PickerItems");
ViewModel
public ObservableCollection<PickerItem> PickerItems { get; set; }
= new ObservableCollection<PickerItem>();
Add Item 함수
private void AddItemToList(PickerItem item)
{
PickerViewModel.PickerItems.Add(item);
if (_tmpList.Count == 0)
{
_tmpList = PickerViewModel.PickerItems.ToList();
}
else
{
_tmpList.Add(item);
}
}
(_tmpList를 통해 초기 목록을 관리함)
* 추가
위의 방법으로 하면 너무 비효율적이고, 복잡하기도 하다.
무언가 ObservableCollection을 수정해야할 때, 새로운 리스트를 만든 후, 새로 초기화해도 된다.
예를들어,
private void SortItemsAtUpdateItem()
{
switch (SortType)
{
case SortType.None:
break;
case SortType.Ascending:
PickerItems = new ObservableCollection<DFNPickerItem>(PickerItems.OrderBy(x => x.ItemText));
var findIndexAscending = PickerItems.ToList().FindIndex(x => x.IsChecked == true);
if (findIndexAscending > -1)
{
SelectedIndex = findIndexAscending;
}
_myCollectionView.ItemsSource = PickerItems;
break;
case SortType.Descending:
PickerItems =
new ObservableCollection<DFNPickerItem>(PickerItems.OrderByDescending(x => x.ItemText));
var findIndexDescending = PickerItems.ToList().FindIndex(x => x.IsChecked == true);
if (findIndexDescending > -1)
{
SelectedIndex = findIndexDescending;
}
_myCollectionView.ItemsSource = PickerItems;
break;
default:
break;
}
}
이런식.
반응형