/// <summary> /// Create js interop wrapper for this c# action /// </summary> /// <param name="callback"></param> /// <param name="serializationSpec"> /// An object specifying the member we'll want from the JS object. /// "new { allChild = "*", onlyMember = true, ignore = false }" will get all the fields in allChild, /// the value of "onlyMember" and will ignore "ignore" /// "true" or null will get everything, false will get nothing /// </param> /// <param name="getJsObjectRef">If true (default false) the call back will get the js object ref instead of the js object content</param> /// <returns>Object that needs to be send to js interop api call</returns> public static CallBackInteropWrapper Create(Func <ValueTask> callback, object serializationSpec = null, bool getJsObjectRef = false) { var res = new CallBackInteropWrapper { CallbackRef = DotNetObjectReference.Create(new JsInteropActionWrapper(callback)), SerializationSpec = serializationSpec, GetJsObjectRef = getJsObjectRef }; return(res); }
/// <summary> /// Create js interop wrapper for this c# action /// </summary> /// <param name="callback"></param> /// <param name="serializationSpec"> /// An object specifying the member we'll want from the JS object. /// "new { allChild = "*", onlyMember = true, ignore = false }" will get all the fields in allChild, /// the value of "onlyMember" and will ignore "ignore" /// "true" or null will get everything, false will get nothing /// </param> /// <param name="getJsObjectRef">If true (default false) the call back will get the js object ref instead of the js object content</param> /// <returns>A wrapper for the event handling</returns> public static CallBackInteropWrapper CreateWithResult <T, TResult>(Func <T, ValueTask <TResult> > callback, object serializationSpec = null, bool getJsObjectRef = false) { var dotnetCallback = DotNetObjectReference.Create(new JsInteropActionWrapperWithResult <T, TResult>(callback)); var res = new CallBackInteropWrapper(dotnetCallback) { SerializationSpec = serializationSpec, GetJsObjectRef = getJsObjectRef, HasResult = typeof(TResult) != typeof(object), HasParameter = typeof(T) != typeof(object), GetArgumentsSerializationAndRef = typeof(ICallbackReferenceData).IsAssignableFrom(typeof(T)) }; return(res); }
/// <summary> /// Add an event listener to the given property and event Type /// </summary> /// <param name="jsRuntime"></param> /// <param name="jsRuntimeObject"></param> /// <param name="propertyName"></param> /// <param name="eventName"></param> /// <param name="callBack"></param> /// <returns></returns> public static async ValueTask <IAsyncDisposable> AddEventListener(this IJSRuntime jsRuntime, JsRuntimeObjectRef jsRuntimeObject, string propertyName, string eventName, CallBackInteropWrapper callBack) { var listenerId = await jsRuntime.InvokeAsync <int>("browserInterop.addEventListener", jsRuntimeObject, propertyName, eventName, callBack).ConfigureAwait(false); return(new ActionAsyncDisposable(async() => await jsRuntime.InvokeVoidAsync("browserInterop.removeEventListener", jsRuntimeObject, propertyName, eventName, listenerId).ConfigureAwait(false))); }