【发布时间】:2016-09-23 15:29:53
【问题描述】:
我有一个从数据库加载数据的详细视图(用于列表选择),我不想通过将数据库逻辑从 UI 线程移开来避免任何阻塞。
问题:
- 使其异步(简单的部分,在完成后调度更新 UI 部分上的 viewmodel 属性的逻辑)
- 当用户(例如,按住 Down-Key)在后台线程完成最后一个条目之前加载新条目时进行处理
- 在这种情况下,之前的加载方法应该是可取消的(例如 CanellationToken)
- 为旧选择启动的调用结果必须被丢弃并且不会到达 UI,尤其是因为如果调用在稍后启动的加载操作之后完成,这可能会覆盖最新数据
如何使用 WPF 做到这一点?如果没有内置方法,我的想法是为此创建一个类并将其用于视图模型中的此类加载方法。例如。想到的两个变种的伪代码
public class ViewModelBackgroundLoader<TInput, TResult>
{
public ViewModelBackgroundLoader(Func<TInput, CancellationToken, TResult> loadFunc, Action<TResult> uiContinuation)
{
}
public void Load(TInput input)
{
// set cancellation for previous loadFunc
// async await loadAction on threadpool thread
// If not cancelled...
// uiContinuation() on UI thread
}
}
public class ViewModelBackgroundLoadedProperty<TInput, TResult> : INotifyPropertyChanged
{
public ViewModelBackgroundLoadedProperty(Func<TInput, CancellationToken, TResult> loadFunc)
{
}
public TInput Input
{
set
{
// set cancellation for running loadFunc
// async await loadAction on threadpool thread
// If not cancelled...
// Update Result property and fire propertychanged (in UI thread)
}
}
public TResult Result { get; }
}
【问题讨论】:
标签: .net wpf asynchronous backgroundworker