与 WPF 属性异步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19550402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:56:17  来源:igfitidea点击:

Async with WPF properties

c#wpfasync-await

提问by ConditionRacer

I have a bound property like this:

我有一个像这样的绑定属性:

public string MyPath
{
    get { return _mypath; }
    set
    {
        _mypath = value;
        NotifyPropertyChanged(() => MyPath);
        LoadSomeStuffFromMyPath(mypath)
    }
}

I'd like the make LoadSomeStuffFromMyPath async but I cannot await from a property. Is there a simple way to do this?

我想要使​​ LoadSomeStuffFromMyPath 异步,但我不能从属性等待。有没有一种简单的方法可以做到这一点?

EDIT: 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.

编辑:不止一个人说过不要使用房产,所以我想说出来。我正在使用 WPF,并且此属性绑定到 UI。我必须使用一个属性。

回答by Reed Copsey

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.

由于您想强制操作在完成之前不继续,我建议创建一种方法来处理加载和显示您的进度。

You could use a separate propertyto disable the UI while this loads, which could effectively "block" your user until the operation completes:

您可以使用单独的属性在加载时禁用 UI,这可以有效地“阻止”您的用户,直到操作完成:

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;
}

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,但让您的 UI 反映操作正在运行(异步)并显示进度等。

回答by dcastro

Perhaps you should move that kind of logic to a method. You're going against the guidelines for using properties:

也许您应该将这种逻辑转移到方法中。您违反了使用属性的准则:

The operation is orders of magnitude slower than a field set would be. If you are even considering providing an asynchronous versionof an operation to avoid blocking the thread, it is very likely that the operation is too expensive to be a property. In particular, operations that access the network or the file system(other than once for initialization) should most likely be methods, not properties.

该操作比字段集慢几个数量级。如果您甚至正在考虑提供操作的异步版本以避免阻塞线程,则该操作很可能过于昂贵而无法成为属性。特别是,访问网络或文件系统的操作(除了一次初始化)最有可能是方法,而不是属性。

http://msdn.microsoft.com/en-us/library/vstudio/ms229054(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/vstudio/ms229054(v=vs.100).aspx

EDIT: I'm not very experienced with WPF, but I think this might be a way to bind to a method instead of a property:

编辑:我对 WPF 不是很有经验,但我认为这可能是一种绑定到方法而不是属性的方法:

Binding a method to a column in datagrid WPF

将方法绑定到数据网格 WPF 中的列