C# 自定义ObservableLink列表无法在我的Windows应用商店应用程序中绑定
我有这个定制的可观察的收藏C# 自定义ObservableLink列表无法在我的Windows应用商店应用程序中绑定,c#,.net,xaml,windows-runtime,windows-store-apps,C#,.net,Xaml,Windows Runtime,Windows Store Apps,我有这个定制的可观察的收藏 public class ObservableLinkedList<T> : INotifyPropertyChanged, INotifyCollectionChanged, IEnumerable<T>, ICollection<T>, ICollection, IEnumerable { public event NotifyCollectionChangedEventHandler CollectionCha
public class ObservableLinkedList<T> : INotifyPropertyChanged, INotifyCollectionChanged, IEnumerable<T>, ICollection<T>, ICollection, IEnumerable
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
private LinkedList<T> _list = new LinkedList<T>();
public ObservableLinkedList()
{
}
public void Clear()
{
_list.Clear();
}
public void Add(T artist)
{
_list.AddLast(artist);
}
public Model.Artist Find(string p)
{
return null;
}
public int Count()
{
return _list.Count();
}
public IEnumerator<T> GetEnumerator()
{
return _list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _list.GetEnumerator();
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
int ICollection<T>.Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
int ICollection.Count
{
get { throw new NotImplementedException(); }
}
public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}
public object SyncRoot
{
get { throw new NotImplementedException(); }
}
}
公共类ObservableLink列表:INotifyPropertyChanged、INotifyCollectionChanged、IEnumerable、ICollection、ICollection、IEnumerable
{
公共事件通知CollectionChangedEventHandler CollectionChanged;
公共事件属性更改事件处理程序属性更改;
私有链接列表_list=新链接列表();
公共可观察性kedlist()
{
}
公共空间清除()
{
_list.Clear();
}
公共空白添加(T艺术家)
{
_列表。AddLast(艺术家);
}
公共模型。艺术家查找(字符串p)
{
返回null;
}
公共整数计数()
{
返回_list.Count();
}
公共IEnumerator GetEnumerator()
{
返回_list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回_list.GetEnumerator();
}
公共布尔包含(T项)
{
抛出新的NotImplementedException();
}
public void CopyTo(T[]数组,int arrayIndex)
{
抛出新的NotImplementedException();
}
int ICollection.Count
{
获取{抛出新的NotImplementedException();}
}
公共图书馆是只读的
{
获取{抛出新的NotImplementedException();}
}
公共布尔删除(T项)
{
抛出新的NotImplementedException();
}
public void CopyTo(数组,int索引)
{
抛出新的NotImplementedException();
}
int ICollection.Count
{
获取{抛出新的NotImplementedException();}
}
公共布尔值是同步的
{
获取{抛出新的NotImplementedException();}
}
公共对象同步根
{
获取{抛出新的NotImplementedException();}
}
}
在某些XAML中绑定:
<Page.DataContext>
<viewModel:SearchViewModel/>
</Page.DataContext>
GridView是这样进行数据绑定的
<GridView ItemsSource="{Binding Path=Artists}" Grid.Row="1">
在这里,我的SearchViewModel定期提供我的ObservableLinkId列表,但UI从未得到更新。可怕的是,如果我用官方的ObservableCollection替换ObservableLink列表,那么它就可以正常工作
CollectionChanged和PropertyChanged始终为空。我已经在clear方法、构造函数和add方法中使用断点检查了这一点
有人能告诉我如何正确地实现这一点吗?一定有办法。事实上,你甚至不需要告诉我如何实现这一点,你可以告诉我一个方向,有人告诉我如何在头盔下绑定。或者如何从XAML中获取更多调试信息
我已尝试删除每个方法中的所有异常抛出和中断
谢谢,
Martin Slot由于您基本上是将项目存储在私有链接列表中,因此您需要自己引发相应的事件 例如:
public void Add(T artist)
{
_list.AddLast(artist);
if (CollectionChanged != null) {
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, Item));
}
}
我没有时间粘贴我所有的代码。如果我这样做,只粘贴相关的零碎部分,我想很多人会把这个问题看得太久 事实上,我刚才已经解决了这个问题。我已将代码更改为:
public class ObservableLinkedList<T> : Collection<T>, INotifyPropertyChanged, INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
private LinkedList<T> _list = new LinkedList<T>();
public ObservableLinkedList()
{
}
public void Clear()
{
_list.Clear();
}
public int Count()
{
return _list.Count();
}
public void Add(T artist)
{
_list.AddLast(artist);
}
public Model.Artist Find(string p)
{
return null;
}
}
公共类ObservableLinkId列表:集合、INotifyPropertyChanged、INotifyCollectionChanged
{
公共事件通知CollectionChangedEventHandler CollectionChanged;
公共事件属性更改事件处理程序属性更改;
私有链接列表_list=新链接列表();
公共可观察性kedlist()
{
}
公共空间清除()
{
_list.Clear();
}
公共整数计数()
{
返回_list.Count();
}
公共空白添加(T艺术家)
{
_列表。AddLast(艺术家);
}
公共模型。艺术家查找(字符串p)
{
返回null;
}
}
将断点放在所有方法中,我可以看到当UI初始化并解析绑定时,CollectionChanged不再为null,这表明UI知道集合。看起来GridView需要的是集合,而不是接口。我尝试实现集合的所有接口,但没有编译代码(这有点奇怪)。编译器抱怨有两个Add方法。我觉得这有点奇怪,但从来没有少过。从集合继承解决了这个问题。现在,我只需实现正确的通知。在连接了调试器的输出窗口中,您是否看到任何数据绑定错误?请提供主模型和所有其他相关位(主模型、定义方式等)。按照Jer的建议尝试调试器-并在getter/setter sb上设置断点,但是CollectionChanged和PropertyChanged的帐户如何始终为空?