简体   繁体   English

如何创建进行异步调用的自定义 PropertyGrid 编辑器?

[英]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.

相关问题 如何创建打开表单的自定义PropertyGrid编辑器项? - How to create custom PropertyGrid editor item which opens a form? 如何创建打开表单的自定义通用 PropertyGrid 编辑器项目? - How to create custom generic PropertyGrid editor item which opens a form? 如何使用 C# 中的另一个(嵌套)编辑器为扩展 WPF 工具包 PropertyGrid 创建自定义属性编辑器? - How to create a custom property editor for Extended WPF Toolkit PropertyGrid with another (nested) editor in C#? PropertyGrid数组编辑器的自定义视图 - Custom view for PropertyGrid array editor 如何为PropertyGrid的自定义属性编辑器添加对多对象编辑的支持? - How to add support of multiobject edits for PropertyGrid's custom property editor? 如何在PropertyGrid自定义集合编辑器中的“添加”按钮下拉列表中自定义名称 - How to customize names in “Add” button dropdown in the PropertyGrid custom collection editor 如何创建将字符串限制为 n 个字符的 PropertyGrid 编辑器 - How to create a PropertyGrid editor that limits a string to n characters PropertyGrid(C#)中布尔属性的自定义编辑器 - Custom Editor for a Boolean Property in a PropertyGrid (C#) Xceed WPF propertyGrid自定义集合编辑器 - Xceed WPF propertyGrid Custom Collection editor 带有自定义编辑器的propertygrid对象,如滑块 - propertygrid object with custom editor like a slider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM