积极答复者
把物理文件拖到控件上会使电脑卡住,怎么解决

问题
-
我拖动一些列的图片文件到wpf的控件上来显示这些图片,但我发现一个问题,在图片没显示完,电脑一直处在卡住状态,知道图像显示完成后,才可以动。个人感觉是电脑进程的问题,其实我只是获取文件的路径,然后根据路径来显示图片,获取文件路径的时间应该很短,应该卡住的时间比较短,但我的貌似时间比较长,那位高手遇到过类似的问题,可以帮小弟指点下迷津,谢谢!
下面获取文件路径的代码:
private void TheTreeViewDrop(object sender, DragEventArgs e)
{
var files = (Array)e.Data.GetData(DataFormats.FileDrop);
this.AddMutiPhotos(this.FiltePicture(files));
}
答案
-
经过本人的努力,这个问题解决了:以下是解决方案,重写ObservableCollection<T>:
public class MTObservableCollection<T> : ObservableCollection<T>
{
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
var eh = CollectionChanged;
if (eh != null)
{
Dispatcher dispatcher = (from NotifyCollectionChangedEventHandler nh in eh.GetInvocationList()
let dpo = nh.Target as DispatcherObject
where dpo != null
select dpo.Dispatcher).FirstOrDefault();
if (dispatcher != null && dispatcher.CheckAccess() == false)
{
dispatcher.Invoke(DispatcherPriority.DataBind, (Action)(() => OnCollectionChanged(e)));
}
else
{
foreach (NotifyCollectionChangedEventHandler nh in eh.GetInvocationList())
nh.Invoke(this, e);
}
}
}
}- 已标记为答案 Hubert_Jingjing Wang 2011年2月28日 5:32
全部回复
-
你好,
我获得文件路径后,异步加载图片时发生这样一个异常,请问这个怎么解决:
首先我有个Photo类,用来存储图片的信息,然后有个Photos的集合,
public class Photos : ObservableCollection<Photo>
{
}我获得文件路径后,添加到这个集合中,却出现这样个异常:
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
以下是我异步加载图片的代码,请帮我看看有啥问题:
private delegate void DFoo(List<string> aa);
private void TheTreeViewDrop(object sender, DragEventArgs e)
{
var files = (Array)e.Data.GetData(DataFormats.FileDrop);
var tt = new DFoo(this.AddMutiPhotos);
IAsyncResult result = tt.BeginInvoke(this.FiltePicture(files), null, null);
while(result.IsCompleted == false)
{
Thread.Sleep(10);
}
tt.EndInvoke(result);
}
-
经过本人的努力,这个问题解决了:以下是解决方案,重写ObservableCollection<T>:
public class MTObservableCollection<T> : ObservableCollection<T>
{
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
var eh = CollectionChanged;
if (eh != null)
{
Dispatcher dispatcher = (from NotifyCollectionChangedEventHandler nh in eh.GetInvocationList()
let dpo = nh.Target as DispatcherObject
where dpo != null
select dpo.Dispatcher).FirstOrDefault();
if (dispatcher != null && dispatcher.CheckAccess() == false)
{
dispatcher.Invoke(DispatcherPriority.DataBind, (Action)(() => OnCollectionChanged(e)));
}
else
{
foreach (NotifyCollectionChangedEventHandler nh in eh.GetInvocationList())
nh.Invoke(this, e);
}
}
}
}- 已标记为答案 Hubert_Jingjing Wang 2011年2月28日 5:32