所以我正在构建一个WPF控件,需要从磁盘加载(并显示)400到600张图像(每张图像大小为200Kb aprox。)。接下来是控件的代码隐藏。
private List<BitmapImage> pages = new List<BitmapImage>();
private BackgroundWorker worker;
public PagesListBox()
{
InitializeComponent();
worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = false;
worker.WorkerReportsProgress = false;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
List<BitmapImage> pagesList = new List<BitmapImage>();
var files = DirectoryServices.GetFiles(Properties.Settings.Default.PagesScanDirectory, new string[] { "*.tiff", "*.jpg", "*.png", "*.bmp" });
foreach (var file in files)
{
Uri uri = new Uri(file);
pagesList.Add(new BitmapImage(uri));
}
e.Result = pagesList;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Pages.ItemsSource = (List<BitmapImage>)e.Result;
}
internal void LoadPages()
{
worker.RunWorkerAsync();
}
internal List<BitmapImage> AttachPages()
{
List<BitmapImage> attachedPages = new List<BitmapImage>();
foreach (BitmapImage eachItem in Pages.SelectedItems)
{
attachedPages.Add(eachItem);
pages.Remove(eachItem);
}
Pages.ItemsSource = null;
Pages.ItemsSource = pages;
return attachedPages;
}
我尝试将页面列表分配给视图(我不能使用后台工作程序),但它失败了。
以其他方式异步加载图像(可能更新UI)或后台工作者的这种方法我正在尝试很好。并且,如果没问题,我该如何解决异常(在已完成的事件中):
Must create DependencySource on same Thread as the DependencyObject.
由于
答案 0 :(得分:2)
要简化代码,请使用ObservableCollection&lt;&gt;而不是List&lt;&gt;并只分配一次ItemsSource属性。
线程错误是因为您正在创建位图并将其添加到后台工作线程而不是主UI线程的列表中。为了解决这个问题,请使用DispatcherHelper(取自Laurent Bugnion的MVVMLight Framwwork)。
我建议你将MVVM视为一种应用程序设计方法,如果你要做很多WPF开发,而不仅仅是以“Winforms方式”做事,尽管这是一种完全不同的思维方式。 / p>
...
DispatcherHelper.Initialise()
...
private ObservableCollection<BitmapImage> _Pages = new ObservableCollection<BitmapImage>();
public PagesListBox()
{
InitializeComponent();
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = false;
worker.WorkerReportsProgress = false;
worker.DoWork += worker_DoWork;
this.ItemSource = _Pages;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
var files = DirectoryServices.GetFiles(Properties.Settings.Default.PagesScanDirectory, new string[] { "*.tiff", "*.jpg", "*.png", "*.bmp" });
foreach (var file in files)
{
DispatcherHelper.CheckBeginInvokeOnUI(()=>
{
Uri uri = new Uri(file);
_Pages.Add(new BitmapImage(uri));
});
}
}
public static class DispatcherHelper
{
public static Dispatcher UIDispatcher { get; private set; }
public static void CheckBeginInvokeOnUI(Action action)
{
if (UIDispatcher.CheckAccess())
action();
else
UIDispatcher.BeginInvoke(action);
}
public static void Initialize()
{
if (UIDispatcher != null)
return;
UIDispatcher = Dispatcher.CurrentDispatcher;
}
}