简体   繁体   English

.NET 核心 - BLAZOR - 自定义组件 - 将异步任务存储在字典中以供以后与 EventCallback 一起使用

[英].NET Core - BLAZOR - Custom component - Store async Task in dictionary for later usage with EventCallback

I have a built a custom Blazor component which have a dictionary with parameters.我有一个定制的 Blazor 组件,它有一个带参数的字典。

It is a common component and is in located in the main layout <SharedComponent></SharedComponent> .它是一个通用组件,位于主布局<SharedComponent></SharedComponent>中。

The component renders another component inside of and passes parameters via the dictionary - in below line of code the dic is parameters .该组件在其中呈现另一个组件并通过字典传递参数 - 在下面的代码行中, dic 是parameters The following code renders the child component.以下代码呈现子组件。

ComponentService.Add<ChildComponent>(text, parameters, taskParameters);

PS: There are two dictionaries: - one for ordinary parameters; PS:有两个字典: - 一个用于普通参数; - one for functions. - 一个用于功能。


How I think this should be achieved:我认为应该如何实现:

  1. Store the async Task into a dictionary with Func delegate.将异步任务存储到具有Func委托的字典中。
  2. Retrieve it later and assign it to multicast delegate.稍后检索它并将其分配给多播委托。
  3. Create new EventCallback and passing the delegate to it.创建新的 EventCallback 并将委托传递给它。
  4. Invoke the callback event passing the return item.调用传递返回项的回调事件。

My question:我的问题:

Is this the correct way at all or there is some other approach?这是完全正确的方法还是有其他方法?


What I have tried so far and I need some help with it...到目前为止我已经尝试过什么,我需要一些帮助......

Here is the dictionary:这是字典:

public class ComponentTaskParameters
{
    private Dictionary<string, Func<Task>> taskPparameters;

    public ComponentTaskParameters()
    {
        taskPparameters = new Dictionary<string, Func<Task>>();
    }

    public void Add(string parameterName, Func<Task> value)
    {
        taskPparameters[parameterName] = value;
    }

    public Func<Task<T>> Get<T>(string parameterName)
    {
        if (!taskPparameters.ContainsKey(parameterName))
        {
            throw new KeyNotFoundException($"{parameterName} does not exist in component parameters");
        }

        return (Func<Task<T>>)taskPparameters[parameterName];
    }
}

Let's say that this is the async function:假设这是异步 function:

async Task SaveItem(Item item)
{
    // save code
}

I tried to store it into the dictionary both wit Func and Action but no luck.我试图用FuncAction将它存储到字典中,但没有运气。

How I can do this and is it achievable at all?我怎么能做到这一点,它是否可以实现?


I created a delegate我创建了一个代表

delegate Item SaveItemDelegate(Func<Task, Item> saveTask);

I am not able to add the retrieved function from the dictionary to the the delegate.我无法将检索到的 function 从字典中添加到委托中。

Here is how I create the EventCallback这是我创建EventCallback的方法

var callbackEvent = new EventCallback<Item>(null, saveItemDelegate);

And I am not able to invoke the callback with CallbackEvent.InvokeAsync(Item);而且我无法使用CallbackEvent.InvokeAsync(Item); . .

Appreciate all the help here.感谢这里的所有帮助。

Thank you!谢谢!

Solution was to store the SaveItem function as multicast delegate in the dictionary.解决方案是将 SaveItem function 作为多播委托存储在字典中。

// Insert into dict function
public void Add(string parameterName, MulticastDelegate value)
{
    taskPparameters[parameterName] = value;
}

// Declaration of the delegate
public delegate Task SaveDelegate(Item item);

// New delegate
SaveDelegate saveDelegate = new SaveDelegate(SaveItem);

// Store the delegate in the dictionary
taskParameters.Add("CallbackFunction", saveDelegate);

Then I was able to retrieve it and pass it to the EventCallback .然后我能够检索它并将其传递给EventCallback Then to invoke it successfully.然后成功调用它。

// Pull out from the dict function
public MulticastDelegate Get(string parameterName)
{       
    return taskPparameters[parameterName];
}

// The delegate
MulticastDelegate save = TaskParameters.Get("CallbackFunction");

// Create new 'EventCallback'
EventCallback callback = new EventCallback(null, save);

// Invoke the 'EventCallback'
callback.InvokeAsync(item);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM