[英]How to create a custom PropertyGrid editor that makes async calls?
I have a custom PropertyGrid editor that launches a form and allows the user to make some selections.我有一个自定义的 PropertyGrid 编辑器,它启动一个表单并允许用户进行一些选择。 These selections are used to provide the property value.
这些选择用于提供属性值。 The form makes a call to a web service to retrieve data for the user to choose from.
该表单调用 web 服务以检索数据以供用户选择。 Currently, this web service call is done synchronously.
目前,此 web 服务调用是同步完成的。 From what I understand, such calls should ideally be made asynchronously, so as not to freeze the application.
据我了解,理想情况下,此类调用应异步进行,以免冻结应用程序。
Is there a way that I could do this asynchronously?有没有办法可以异步执行此操作?
It seems like I would need to make my EditValue method async, but the class that I am over-riding (UITypeEditor) does not appear to have this option.似乎我需要使我的 EditValue 方法异步,但是我覆盖的 class (UITypeEditor)似乎没有这个选项。
This answer provides a nice background for how I have my custom PropertyGrid editor implemented.这个答案为我如何实现我的自定义 PropertyGrid 编辑器提供了一个很好的背景。 However, I will also provide a very basic code snippet to provide a rough idea of what I am trying to do.
但是,我还将提供一个非常基本的代码片段,以大致了解我正在尝试做什么。 I'm not using a form in this example, but the important thing to understand is that I am trying to await an async call within my EditValue method.
在此示例中,我没有使用表单,但要了解的重要一点是,我正在尝试在我的 EditValue 方法中等待异步调用。
class FooEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
using (var client = MasterContainer.Resolve<ValueWebClient>())
{
var value = await client.GetCurrentValue(); // visual studio shows await can only be used in async method error
return value;
}
}
}
I suppose one approach might be to load all of your data up-front so that you don't have to make the call.我想一种方法可能是预先加载所有数据,这样您就不必打电话了。 However, the data could be changed while the user is working, so I would prefer not to take that approach.
但是,数据可能会在用户工作时更改,所以我不希望采用这种方法。
You can't use the async
keyword in the EditValue
method without changing the return type of the method to Task<object>
.如果不将方法的返回类型更改为
Task<object>
,则不能在EditValue
方法中使用async
关键字。
Assuming you own the UITypeEditor
base class, this would be a good option.假设您拥有
UITypeEditor
基础 class,这将是一个不错的选择。
The current API ( object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value
) is indeed synchronous though and it's not much you can do about it I am afraid. Loading all data up-front seems to be a good compromise.当前的 API (
object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value
) 确实是同步的,尽管我似乎是一个很好的妥协。预先加载数据似乎不太好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.