异步与WPF性能 [英] Async with WPF properties
问题描述
我有一个绑定属性是这样的:
I have a bound property like this:
public string MyPath
{
get { return _mypath; }
set
{
_mypath = value;
NotifyPropertyChanged(() => MyPath);
LoadSomeStuffFromMyPath(mypath)
}
}
我想化妆LoadSomeStuffFromMyPath异步,但我不能从属性等待。有一个简单的方法来做到这一点?
I'd like the make LoadSomeStuffFromMyPath async but I cannot await from a property. Is there a simple way to do this?
编辑:不止一个人曾表示,不使用属性,所以我想打电话给了这一点。我使用WPF和这个属性绑定到用户界面。我必须使用一个属性。
More than one person has said not to use a property so I want to call this out. I am using WPF and this property is bound to the UI. I have to use a property.
推荐答案
由于要强制执行该操作不继续下去,直到这个完成后,我会建议作出处理负载的方法和显示你的进步。
Since you want to force the operation to not continue until this is complete, I would suggest making a method to handle loading and displaying your progress.
您可以使用的一个单独的属性的禁用UI,而这种负载,从而可以有效地堵你的用户,直到操作完成:
You could use a separate property to disable the UI while this loads, which could effectively "block" your user until the operation completes:
public string MyPath
{
get { return _mypath; }
set
{
_myPath = value;
NotifyPropertyChanged(() => MyPath);
UpdateMyPathAsync();
}
}
public async Task UpdateMyPathAsync()
{
this.EnableUI = false; // Stop user from setting path again....
await LoadSomeStuffFromMyPathAsync(MyPath);
this.EnableUI = true;
}
这允许你还是绑定,如正常,WPF,但你的UI反映操作运行(异步),并显示进度等。
This allows you to still bind, as normal, with WPF, but have your UI reflect that the operation is running (asynchronously) and display progress, etc.
这篇关于异步与WPF性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!