zoukankan      html  css  js  c++  java
  • 简单记录几个wpf学习上的问题[ObservableQueue]

    我想给我的程序加一个下载队列,当我点击一个下载按钮的时候,他应该把这个插件信息(对象)加到一个队列中,然后队列里去实现下载和删除任务,下载完成则删除对象

    首先我想到了Queue类型,然后我在我的viewmodel里定义了

            private Queue<AddonDisplay> _addonQueue;
            public Queue<AddonDisplay> AddonQueue
            {
                get { return _addonQueue; }
                set { SetProperty(ref _addonQueue, value); }
            }
    

    当我向其添加Enqueue元素的时候,发现没有效果!
    一顿搜索,找到了两篇文章:
    https://stackoverflow.com/questions/3127136/observable-stack-and-queue
    https://stackoverflow.com/questions/40019395/implementing-own-observablecollection

    原来是要实现:INotifyCollectionChanged, INotifyPropertyChanged两个接口!

    PS. 其实在Google之前应该可以先看一眼ObservableCollection类的定义

    最终实现代码如下:(我继承了ConcurrentQueue,一个从.net4开始出现的线程安全的queue)

        class ObservableQueue<T> : ConcurrentQueue<T>, INotifyPropertyChanged, INotifyCollectionChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public event NotifyCollectionChangedEventHandler CollectionChanged;
            public new void Enqueue(T item)
            {
                base.Enqueue(item);
                CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
    
            public new bool TryDequeue(out T result)
            {
                bool successed = base.TryDequeue(out result);
                if (successed)
                    CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                return successed;
    
            }
        }
    

    这里有个小问题就是,我上面Enqueue的时候想使用NotifyCollectionChangedAction.Add,编译不通过,没仔细研究,反正这个Queue是用起来了。

    然后在 viewmodel里将上面的Queue定义改成:

         private ObservableQueue<AddonDisplay> _addonQueue;
            public ObservableQueue<AddonDisplay> AddonQueue
            {
                get { return _addonQueue; }
                set { SetProperty(ref _addonQueue, value); }
    
            }
    

    就可以了!

  • 相关阅读:
    并行fp-growth图解(mahout)
    Sqoop的安装与使用
    深入理解Hadoop集群和网络
    datanode与namenode的通信原理
    Hadoop添加节点datanode(生产环境)
    Hadoop中HDFS工作原理
    实现hadoop中的机架感知
    hadoop集群监控工具Apache Ambari安装配置教程
    sdn测量综述
    SDN测量论文粗读(三)9.24
  • 原文地址:https://www.cnblogs.com/hicolin/p/13879286.html
Copyright © 2011-2022 走看看