Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加入;; Application.Current.Dispatcher.BeginInvoke(addMethod,item); } }_C#_.net_Wpf - Fatal编程技术网

C# 加入;; Application.Current.Dispatcher.BeginInvoke(addMethod,item); } }

C# 加入;; Application.Current.Dispatcher.BeginInvoke(addMethod,item); } },c#,.net,wpf,C#,.net,Wpf,编辑: 我从stackoverflow的帖子中偷了它,但忘了是从哪个帖子中偷来的。我认为,如果需要考虑模型层中的线程,就必须进行很多耦合 您应该做的是不要将模型直接连接到GUI。正如其他人所说,使用中间层(MVVM) 这意味着您可以让MVVM层响应来自可观察集合的更改通知。MVVM层决定是否以及如何将这些通知传递给GUI。寻找一种降低GUI更新频率的方法,以保持其可用性 简言之: 如果愿意,请在模型层中继续使用ObeservableCollection,但不要直接在GUI绑定中使用它。让另一层接

编辑:
我从stackoverflow的帖子中偷了它,但忘了是从哪个帖子中偷来的。我认为,如果需要考虑模型层中的线程,就必须进行很多耦合

您应该做的是不要将模型直接连接到GUI。正如其他人所说,使用中间层(MVVM)

这意味着您可以让MVVM层响应来自可观察集合的更改通知。MVVM层决定是否以及如何将这些通知传递给GUI。寻找一种降低GUI更新频率的方法,以保持其可用性

简言之:
如果愿意,请在模型层中继续使用ObeservableCollection,但不要直接在GUI绑定中使用它。让另一层接收通知并控制GUI更新。

+1因为您从Jason:)引用了MVVM上的精彩视频。我想我会投票给每一个有链接的答案。再怎么强调它有多好也不为过。嗯,你的答案是正确的。呵呵,我想不起来我在哪里见过类似的东西+1关于此选项的详细信息:)+1
// somewhere in thread pool:
for(int i = 0; i < 1000; i++)
{
   _dispatcherAwareCollection.Add(i);
}

public class WrapperClass<T>
{
   public ObservableCollection<T> Collection {get; set;}

   public void Add(T item) 
   { 
      //do your dispatcher magic here 
   }
   ...
}
public class AsyncHelper
{
    public static void EnsureUIThread(Action action) 
    {
        if (Application.Current != null && !Application.Current.Dispatcher.CheckAccess()) 
        {
            Application.Current.Dispatcher.BeginInvoke(action, DispatcherPriority.Background);
        }
        else 
        {
            action();
        }
    }
}
AsyncHelper.EnsureUIThread(() =>
{
    // Update you observable collections here
});
 public static class DispatcherInvoker
 {       

    public static void AddOnUI<T>(this ICollection<T> collection, T item)
    {
        Action<T> addMethod = collection.Add;
        Application.Current.Dispatcher.BeginInvoke(addMethod, item);
    }
 }