简体   繁体   中英

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

It is a common component and is in located in the main layout <SharedComponent></SharedComponent> .

The component renders another component inside of and passes parameters via the dictionary - in below line of code the dic is parameters . The following code renders the child component.

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

PS: There are two dictionaries: - one for ordinary parameters; - one for functions.


How I think this should be achieved:

  1. Store the async Task into a dictionary with Func delegate.
  2. Retrieve it later and assign it to multicast delegate.
  3. Create new EventCallback and passing the delegate to it.
  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:

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

I tried to store it into the dictionary both wit Func and Action but no luck.

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.

Here is how I create the EventCallback

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

And I am not able to invoke the callback with CallbackEvent.InvokeAsync(Item); .

Appreciate all the help here.

Thank you!

Solution was to store the SaveItem function as multicast delegate in the dictionary.

// 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 . 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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