簡體   English   中英

Blazor:對EventCallback使用動態參數時,無效的參數類型匹配異常<T>

[英]Blazor: Invalid argument type match exception when using dynamic for EventCallback<T>

我有一個自定義組件,其中有多個事件,每個事件都有唯一的T類型。 然后JSInvokable方法是常見的方法(入口點),在這里我需要從中調用確切的事件函數處理程序。

這樣做時,我需要將參數和函數處理程序轉換為適當的類型。 但是由於類型轉換問題,我使用了動態類型。

但是,盡管傳遞了適當的參數類型,但在運行時仍無法解決問題。

請檢查拋出的錯誤截圖:

![image] [ https://user-images.githubusercontent.com/12065858/62135248-84725200-b2ff-11e9-8624-cbcae3193151.png]

Comp.razor


@using Typecasting
@using System.Threading.Tasks;
@using Newtonsoft.Json;

@inherits Base;

<input id="gencomp" type="text" />



@code {

    [Parameter]
    public EventCallback<ChangeEventArgs> ValueChange
    {
        get { return (EventCallback<ChangeEventArgs>)this.GetEvent("change"); }
        set { this.SetEvent<ChangeEventArgs>("change", value); }
    }

    [Parameter]
    public EventCallback<FocusOutEventArgs> FocusOut
    {
        get { return (EventCallback<FocusOutEventArgs>)this.GetEvent("blur"); }
        set { this.SetEvent<FocusOutEventArgs>("blur", value); }
    }

    public async Task<string> DummyCall()
    {
        // dummy async action method to show case the issue
        return await Task.Run(() => { return "data"; });
    }

    [JSInvokable]
// this is entry point 
    public override object Trigger(string eventName, string arg)
    {

        EventData data = this.DelegateList[eventName];
        var eventarg = JsonConvert.DeserializeObject(arg, data.ArgumentType);
        dynamic fn = data.Handler;
// here am getting the issue
        fn.InvokeAsync(eventarg);
        return eventarg;
    }    

}

base.cs

 public Dictionary<string, EventData> DelegateList = new Dictionary<string, EventData>();
 internal virtual void SetEvent<T>(string name, EventCallback<T> eventCallback)
        {
            if (this.DelegateList.ContainsKey(name))
            {
                this.DelegateList[name] = new EventData().Set<T>(eventCallback, typeof(T));
            }
            else
            {
                this.DelegateList.Add(name, new EventData().Set<T>(eventCallback, typeof(T)));
            }
        }

        internal  object GetEvent(string name)
        {
            if (this.DelegateList.ContainsKey(name) == false)
            {
                return null;
            }
            return this.DelegateList[name].Handler;
        }

------

EventData類

public class EventData
    {
        public object Handler { get; set; }

        public Type ArgumentType { get; set; }

        public EventData Set<T>(EventCallback<T> action, Type type)
        {
            this.Handler = action;
            this.ArgumentType = type;
            return this;
        }

    }

您可以從此處找到問題重現示例。

https://github.com/gurunathancs1991/BlazorEventhandler

動態類型的eventCallback轉換是否存在問題? 還有其他解決方法嗎?

更改:

get { return (EventCallback<ChangeEventArgs>)this.GetEvent("change"); }

至:

get { return (EventCallback<ChangeEventArgs>)(object) this.GetEvent("change"); }

也許現在您不需要使用動態...

祝好運...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM