【问题标题】:Custom ObservableLinkedList would not bind in my Windows store app自定义 ObservableLinkedList 不会绑定在我的 Windows 商店应用程序中
【发布时间】:2013-04-02 20:09:50
【问题描述】:

我有这个自定义的 observable 集合

    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(); }
    }
}

在某些 XAML 中绑定:

    <Page.DataContext>
    <viewModel:SearchViewModel/>
</Page.DataContext>

GridView 就是这样的数据绑定

<GridView ItemsSource="{Binding Path=Artists}" Grid.Row="1">

我的 SearchViewModel 在术语中分发我的 ObservableLinkedList,但 UI 永远不会得到更新。可怕的是,如果我用官方的 ObservableCollection 替换我的 ObservableLinkedList,那么它就可以正常工作了。

CollectionChanged 和 PropertyChanged 始终为空。我已经在 clear 方法、构造函数和 add 方法中使用断点检查了这一点。

有人可以告诉我如何正确实施吗?一定有什么办法。事实上,您甚至不必告诉我如何实现这一点,您只需向我指出一个方向,有人告诉我如何在头盔下完成绑定。或者如何从 XAML 中获取更多调试信息。

我已尝试消除所有方法中的所有异常抛出和中断。

谢谢, 马丁槽

【问题讨论】:

  • 您是否在附加调试器的输出窗口中看到任何数据绑定错误?
  • 给我们主要模型和所有其他相关位(主要模型,你如何定义等)。按照 Jer 的建议尝试调试器 - 并在 getter/setter-s 上设置断点

标签: c# .net xaml windows-runtime windows-store-apps


【解决方案1】:

由于您基本上是将项目存储在私有链表中,因此您需要自己引发适当的事件。

例如:

public void Add(T artist)
{
    _list.AddLast(artist);
     if (CollectionChanged != null) {
         CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, Item));
    }
}

【讨论】:

  • 但是,CollectionChanged 和 PropertyChanged 始终为 null 的原因是什么?
【解决方案2】:

我没有时间粘贴我的所有代码。如果我这样做,并且只粘贴相关的点点滴滴,我想很多人会认为这个问题很长。

事实上,我刚才已经解决了这个问题。我已将代码更改为:

    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;
    }
}

在所有方法中放置断点,我可以看到当 UI 初始化并看到解决了绑定时,CollectionChanged 不再为空,表明 UI 知道集合。看起来 GridView 想要一个集合而不是接口。我试图实现 Collection 的所有接口,但没有编译代码的运气(这有点奇怪)。编译器抱怨有两个 Add 方法。我觉得这有点奇怪,但从来没有少过。从 Collection 继承解决了这个问题。现在我只需要实现正确的通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多