[英].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:我认为应该如何实现:
Func
delegate.Func
委托的字典中。 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.我试图用
Func
和Action
将它存储到字典中,但没有运气。
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.