【发布时间】:2015-04-08 06:52:55
【问题描述】:
我有一个 WPF 表单,它的屏幕被分成 2 个部分作为主详细信息表单。上面的部分是主人,它显示了一个数据网格中的一堆记录,这些记录是数据绑定到一个collectionviewsource。 每当用户单击一行时,表单的底部都是通过对 collectionviewsource.current_changed 事件做出反应而构建的。 在那里,我正在取消任何未决的异步操作,并开始一个新的操作。 代码是这样的:
if (_tokenSource != null) //_tokenSource is an instance variable of the class that implements current_changed
{
try
{
_tokenSource.Cancel(); //needed because _tokenSource might be disposed already. Ugly.
}
catch (Exception Ex)
{
}
}
using (_tokenSource = new CancellationTokenSource())
{
try
{
_unitOfWork = await loadRelatieAsync(relatieId, _tokenSource.Token); // this is just currently an await Task.Delay(5000,token).ConfigureAwait(true); return null;
}
catch (Exception Ex)
{
}
}
//_tokenSource = null; can’t do this, it would lead to several operations not being cancelled
我在这里的东西似乎可以工作,但是代码很难看,应用程序仍然有点迟钝。有没有合适/更好的方法来做到这一点?
【问题讨论】:
标签: wpf mvvm async-await collectionviewsource