当需要用Lisbbox 来log 一些记录的时候,ObservableCollection 并不可以是记录实时的反应在WPF 的UI上面。

这个时候就需要用一个异步collection 来完成。

    /// <summary>
/// Represents the asynchronous observable collection.
/// </summary>
/// <typeparam name="T"></typeparam>
public class AsyncObservableCollection<T> : ObservableCollection<T>
{
/// <summary>
/// The _synchronization context
/// </summary>
private readonly SynchronizationContext synchronizationContext = SynchronizationContext.Current; /// <summary>
/// Initializes a new instance of the <see cref="AsyncObservableCollection{T}"/> class.
/// </summary>
public AsyncObservableCollection()
{
} /// <summary>
/// Initializes a new instance of the <see cref="AsyncObservableCollection{T}"/> class.
/// </summary>
/// <param name="list">The list.</param>
public AsyncObservableCollection(IEnumerable<T> list)
: base(list)
{
} /// <summary>
/// Inserts the item.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="item">The item.</param>
protected override void InsertItem(int index, T item)
{
this.ExecuteOnSyncContext(() => base.InsertItem(index, item));
} /// <summary>
/// Removes the item.
/// </summary>
/// <param name="index">The index.</param>
protected override void RemoveItem(int index)
{
this.ExecuteOnSyncContext(() => base.RemoveItem(index));
} /// <summary>
/// Sets the item.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="item">The item.</param>
protected override void SetItem(int index, T item)
{
this.ExecuteOnSyncContext(() => base.SetItem(index, item));
} /// <summary>
/// Moves the item.
/// </summary>
/// <param name="oldIndex">The old index.</param>
/// <param name="newIndex">The new index.</param>
protected override void MoveItem(int oldIndex, int newIndex)
{
this.ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
} /// <summary>
/// Clears the items.
/// </summary>
protected override void ClearItems()
{
this.ExecuteOnSyncContext(() => base.ClearItems());
} /// <summary>
/// Executes the on synchronize context.
/// </summary>
/// <param name="action">The action.</param>
private void ExecuteOnSyncContext(Action action)
{
if (SynchronizationContext.Current == this.synchronizationContext)
{
action();
}
else
{
this.synchronizationContext.Send(_ => action(), null);
}
}
}

另外还需要启用一个新的线程来更新collection

后续补充:

UI 编程中只要搞清楚如下两点:

1. UI 线程(主线程,入Dispatcher队,只用于更新UI), 在UI线程里面不要做耗时的操作,所有UI线程里面都是用于更新UI的。

2. 所有的耗时操作全部放到另外的线程里去做。

搞清楚如上两点,这个Listbox的更新可以通过如下代码来实现了。

           // Start a non-UI thread to do some works without blocking UI thread.
Task.Factory.StartNew(() =>
{
for (int i = ; i < ; i++)
{
// Put UI change enter dispatcher queue.
this.Dispatcher.Invoke(() =>
{
((LoggerListBoxViewModel)(this.DataContext)).Logger.Add("Log~~~~.." + i);
}); // Other works.
Thread.Sleep();
}
});

在ViewModel裏面可以如下更新:

  private void LogMessage(string message)
{
// Start a non-UI thread to do some works without blocking UI thread.
Task.Factory.StartNew(() =>
{
// Put UI change enter dispatcher queue.
Application.Current.Dispatcher.Invoke(() =>
{
this.Logger.Add(message);
});
});
}

注意,如上,Disparter.Invoke是UI线程,里面不能做耗时的工作。仅仅用于更新 UI。

(MVVM) ListBox Binding 和 实时刷新的更多相关文章

  1. 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令

    [源码下载] 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 作者:webabcd ...

  2. 背水一战 Windows 10 (23) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令

    [源码下载] 背水一战 Windows 10 (23) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令 作者:webabcd ...

  3. 使用SignalR实现比特币价格实时刷新

    ASP.NET SignalR是微软支持的一个运行在 Dot NET 平台上的 HTML Websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重 ...

  4. Sublime Text 3配置LiveReload实现实时刷新

    今天看到一款很强大的插件,LiveReload,实时刷新,也就是说写完html/css/js等不用再到浏览器里按F5啦,在Ctrl+S时浏览器会自动刷新,是不是想想都很爽... Chrome:(据说支 ...

  5. Gulp-livereload:实时刷新编码

    实现功能 监听指定目录下的所有文件,实时动态刷新页面 安装(Install) 功能的实现是借助 gulp-connect 插件完成的;所以,首先通过下面命令完成插件安装: npm install -- ...

  6. tab栏切换,内容为不断实时刷新数据的vue实现方法

    先说一下产品需求,就是有几个tab栏,每个tab栏对应的ajax请求不一样,内容区域一样,内容为实时刷新数据,每3s需要重新请求,返回的数据在内容区域展示,每点击一次tab栏需停止其他tab栏ajax ...

  7. webpack-dev-server 搭建本地服务以及浏览器实时刷新

    一.概述开发项目中为了保证上线,开发项目是都需要使用localhost进行开发,以前的做法就是本地搭建Apache或者Tomcat服务器.有的前端开发人员 对服务器的搭建和配置并不熟悉,这个时候需要后 ...

  8. JAVAFX之tableview界面实时刷新导致的内存溢出(自己挖的坑,爬着也要出来啊0.0)

    这几天遇到了一个问题,不幸开发的一个cs架构的工具,客户端开启后,内存一直在缓慢增长最终导致进程卡死,花了4天时间,终于爬出来了... 客户端通过timer定时器每30秒查询一次数据库以及一些业务逻辑 ...

  9. js获取当前时间并实时刷新

    效果如图: 代码如下: <html> <head> <title>js获取当前时间并实时刷新</title> <script> //页面加载 ...

随机推荐

  1. 多个tomcat一起运行

    1.默认为:8005 2.在D:\apache-tomcat-7.0.85\bin目录下,找到startup.bat,打开并修改. 3.在D:\apache-tomcat-7.0.85\bin目录下, ...

  2. 更改Android编译软件版本(make/gcc/bision)

    一.make版本 1.下载make的压缩包 ftp://ftp.gnu.org/gnu/make/ 2.解压,安装 cd make-x.x ./configuration sh build.sh su ...

  3. [转] Vagrant入门

    [From] https://www.cnblogs.com/davenkin/p/vagrant-virtualbox.html 简单地说,Vagrant让我们可以通过代码的方式快速地.可重复地创建 ...

  4. python 继承与组合

    一.组合 #老师 课程 生日 class Course: def __init__(self,name,period,price): self.name = name self.period = pe ...

  5. TestGc finalize()

    package com.gc; public class TestGc { public static void main(String[] args) { Man man = new Man(&qu ...

  6. UGUI Slider的onValueChanged事件

    在本文,你将学到如何将UGUI Slider的onValueChanged事件进行统一管理. using System; using UnityEngine; using UnityEngine.UI ...

  7. 迁移-Mongodb时间类数据比较的坑

    背景: 拦截件监控时,对于签收的数据需要比较签收时间和实际同步数据的时间来判断  同步时间是在签收前还是签收后.在比较时,用到同步时间syncTime和signTime, signTime从Q9查单获 ...

  8. iview modal对话框的一个小知识点

    我在做一个项目的一个页面的时候有两个弹窗,发现点击一个弹窗的右上角关闭按钮后,打开另一个弹窗的时候,另一个弹窗里面带有上一个弹窗里的样式内容,说明上一个弹窗并没有被销毁. 后来发现modal源码里的c ...

  9. Oracle DMP

    通过DMP对Oracle数据库进行导入导出 打开“开始”--->输入cmd,打开cmd命令窗口,输入以下命令即可 1导出 (1)将数据库ORACLE完全导出,用户名system密码manager ...

  10. android中Zing二维码扫描,二维码生成

    Android中二维码扫描的最常用库是zxing和zbar,zxing项目地址为https://github.com/zxing/zxing,目前还有多个人在维护.zbar主要用C来写的,对速度有要求 ...