Xamarin 手动引发要刷新的可观察列表的属性更改
我可以看到新联系人已添加到列表中,但列表仍然没有正确排列。我是否需要手动触发OnProperty更改以刷新可观察列表?我不确定您要做什么。但是从代码中我发现你在其他地方更新了列表 我看到的是,您将Xamarin 手动引发要刷新的可观察列表的属性更改,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我可以看到新联系人已添加到列表中,但列表仍然没有正确排列。我是否需要手动触发OnProperty更改以刷新可观察列表?我不确定您要做什么。但是从代码中我发现你在其他地方更新了列表 我看到的是,您将联系人引用重置为新的可观察列表。这可能会破坏数据绑定,导致UI无法更新 检索新列表,清除当前的可观察集合,然后重新填充它。或者,再次设置数据绑定属性,但这将导致再次计算所有绑定。来自我的自定义公共类NiftyObservableCollection:ObservableAgeCollection //-
联系人
引用重置为新的可观察列表
。这可能会破坏数据绑定,导致UI无法更新
检索新列表,清除当前的
可观察集合
,然后重新填充它。或者,再次设置数据绑定
属性,但这将导致再次计算所有绑定。来自我的自定义公共类NiftyObservableCollection:ObservableAgeCollection
//-----------------------------------------------------------------------------------------------------------
公共作废报告项变更(T项)
//-----------------------------------------------------------------------------------------------------------
{
NotifyCollectionChangedEventArgs参数=
新NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace,
项目
项目
指数(项目);;
OnCollectionChanged(args);
}
//---------------------------------------------------------------------------------------------------------
public void RefreshRange(int索引、IEnumerable集合)
//---------------------------------------------------------------------------------------------------------
{
检查可重入性();
var changedItems=集合是列表?(列表)集合:新列表(集合);
对于(var currentIndex=index;currentIndex
对不起,我是Xamarin的新手。是的,我正在将ObservableList传递给另一个视图模型。原始的可观察列表是静态的,所以我假设我仍然引用它?我可以看到新联系人已添加到原始observable列表属性中,但未排列。在我的排列联系人中发现问题,我正在返回observable的新实例。很抱歉浪费了你的时间:)这就是我想说的:)没问题,很乐意帮忙!
private static ObservableCollection<Contact> _contacts;
public ObservableCollection<Contact> Contacts
{
get { return _contacts; }
set
{
_contacts = value;
OnPropertyChanged("Contacts");
}
}
public async void OnSave()
{
Contacts.Add(Contact);
Contacts = Utility.ArrangeContacts(Contacts);
await Navigation.PopAsync();
}
//-----------------------------------------------------------------------------------------------------------
public void ReportItemChange(T item)
//-----------------------------------------------------------------------------------------------------------
{
NotifyCollectionChangedEventArgs args =
new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Replace,
item,
item,
IndexOf(item));
OnCollectionChanged(args);
}
//---------------------------------------------------------------------------------------------------------
public void RefreshRange(int index, IEnumerable<T> collection)
//---------------------------------------------------------------------------------------------------------
{
CheckReentrancy();
var changedItems = collection is List<T> ? (List<T>)collection : new List<T>(collection);
for (var currentIndex = index; currentIndex < changedItems.Count+index; currentIndex++ )
{
try
{
var i = changedItems[currentIndex];
Items.RemoveAt(currentIndex); //remove
Items.Insert(currentIndex, i); //insert new
}
catch
{
break;
}
}
// OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, changedItems, changedItems, index));
}