コード例 #1
0
        /// <summary>
        ///  listen for dispatched messages send by PostMessage
        /// </summary>
        /// <param name="todo"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async ValueTask <IAsyncDisposable> OnMessage <T>(Func <MessageEvent <T>, ValueTask> todo)
        {
            return(await JsRuntime.AddEventListener(
                       JsObjectRef,
                       "",
                       "message",
                       CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                           async payload =>
            {
                var eventPayload = new MessageEvent <T>
                {
                    Data = await JsRuntime.GetInstanceProperty <T>(payload, "data").ConfigureAwait(false),
                    Origin = await JsRuntime.GetInstanceProperty <string>(payload, "origin")
                             .ConfigureAwait(false),
                    Source = await JsRuntime.GetInstanceProperty <WindowInterop>(payload, "source",
                                                                                 SerializationSpec).ConfigureAwait(false)
                };
                eventPayload.Source.SetJsRuntime(JsRuntime,
                                                 await JsRuntime.GetInstancePropertyRef(payload, "source").ConfigureAwait(false));

                await todo.Invoke(eventPayload).ConfigureAwait(false);
            },
                           getJsObjectRef: true
                           )).ConfigureAwait(false));
        }
コード例 #2
0
 /// <summary>
 ///  fire when a window is about to unload its resources. At this point, the document is still visible and the event is still cancelable.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnBeforeUnload(Func <BeforeUnloadEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(
                JsObjectRef, "",
                "beforeunload",
                CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                    async e => { await callback.Invoke(new BeforeUnloadEvent(JsRuntime, e)); },
                    getJsObjectRef: true)
                ));
 }
コード例 #3
0
 /// <summary>
 /// raised when the window is closed
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnContextMenu(Func <CancellableEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(
                JsObjectRef, "",
                "contextmenu",
                CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                    async e => { await callback.Invoke(new CancellableEvent(JsRuntime, e)); },
                    getJsObjectRef: true)
                ));
 }
コード例 #4
0
 /// <summary>
 /// queues a function to be called during a browser's idle periods. This enables developers to perform background and low priority work on the main event loop, without impacting latency-critical events such as animation and input response.
 /// </summary>
 /// <param name="callback">ells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.</param>
 /// <param name="options">Contains optional configuration parameters. Currently only one property is defined : timeout. If timeout is specified and has a positive value, and the callback has not already been called by the time timeout milliseconds have passed, the callback will be called during the next idle period, even if doing so risks causing a negative performance impact.
 /// </param>
 /// <returns>The request ID, can be used for cancelling it</returns>
 public async ValueTask <int> RequestIdleCallback(Func <IdleDeadline, ValueTask> callback,
                                                  RequestIdleCallbackOptions options = null)
 {
     return(await JsRuntime.InvokeInstanceMethod <int>(JsObjectRef, "requestIdleCallback",
                                                       CallBackInteropWrapper.Create <JsRuntimeObjectRef>(async jsRef =>
     {
         var idleDeadline = await JsRuntime.GetInstanceContent <IdleDeadline>(jsRef, true);
         idleDeadline.SetJsRuntime(JsRuntime, jsRef);
         await callback.Invoke(idleDeadline);
     }, getJsObjectRef: true), options));
 }
コード例 #5
0
        /// <summary>
        /// Create js interop xrapper for this c# action.static
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="getDeepObject">If true then the event payload are serialized deeply, if no it's only shallow (mandatory when there is a window object)</param>
        /// <param name="payloadPropertiesPathByRef">List of property path that needs to be send a js object ref instead of js object serialized/deseriliazed</param>
        /// <returns>Object that needs to be send to js interop api call</returns>
        public static CallBackInteropWrapper Create(Func <Task> callback, bool getDeepObject = true, bool getJsObjectRef = false)
        {
            var res = new CallBackInteropWrapper
            {
                CallbackRef    = DotNetObjectReference.Create(new JSInteropActionWrapper(callback)),
                GetDeepObject  = getDeepObject,
                GetJsObjectRef = getJsObjectRef
            };

            return(res);
        }
コード例 #6
0
 /// <summary>
 /// fires when a window's hash changes
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnHashChange(Func <ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(
                JsObjectRef, "",
                "hashchange",
                CallBackInteropWrapper.Create(
                    async() => { await callback.Invoke(); },
                    getJsObjectRef: false,
                    serializationSpec: false
                    )
                ));
 }
コード例 #7
0
 public async ValueTask <IAsyncDisposable> OnStorage(Func <StorageEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "storage",
                                             CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                                                 async jsObject =>
     {
         var eventContent = await JsRuntime.GetInstanceContent <StorageEvent>(jsObject,
                                                                              new { key = true, oldValue = true, newValue = true, url = true });
         eventContent.Storage = new WindowStorage(JsRuntime,
                                                  await JsRuntime.GetInstancePropertyRef(jsObject, "storageArea"));
         await callback(eventContent);
     },
                                                 getJsObjectRef: true
                                                 )
                                             ));
 }
コード例 #8
0
 /// <summary>
 /// fired at a regular interval and indicates the amount of physical force of acceleration the device is receiving at that time. It also provides information about the rate of rotation, if available.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnDeviceMotion(Func <DeviceMotionEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(
                JsObjectRef, "",
                "devicemotion",
                CallBackInteropWrapper.Create <DeviceMotionEvent>(
                    async e => { await callback.Invoke(e); },
                    getJsObjectRef: false,
                    serializationSpec: new
     {
         acceleration = "*",
         accelerationIncludingGravity = "*",
         rotationRate = "*",
         interval = "*"
     })
                ));
 }
コード例 #9
0
 /// <summary>
 /// fired when fresh data is available from an orientation sensor about the current orientation of the device as compared to the Earth coordinate frame. This data is gathered from a magnetometer inside the device
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnDeviceOrientation(Func <DeviceOrientationEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(
                JsObjectRef, "",
                "deviceorientation",
                CallBackInteropWrapper.Create <DeviceOrientationEvent>(
                    async e => { await callback.Invoke(e); },
                    getJsObjectRef: false,
                    serializationSpec: new
     {
         alpha = "*",
         beta = "*",
         gamma = "*",
         absolute = "*"
     })
                ));
 }
コード例 #10
0
 public async Task <IAsyncDisposable> OnBeforeInstallPrompt(Func <BeforeInstallPromptEvent, Task> callback)
 {
     return(await jsRuntime.AddEventListener(
                JsRuntimeObjectRef, "",
                "beforeinstallprompt",
                CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                    async jsObjectRef =>
     {
         BeforeInstallPromptEvent beforeInstallPromptEvent = new BeforeInstallPromptEvent();
         beforeInstallPromptEvent.Platforms = await jsRuntime.GetInstancePropertyAsync <string[]>(jsObjectRef, "platforms");
         beforeInstallPromptEvent.SetJsRuntime(jsRuntime, jsObjectRef);
         await callback.Invoke(beforeInstallPromptEvent);
     },
                    getJsObjectRef: true,
                    getDeepObject: false
                    )
                ));
 }
コード例 #11
0
 /// <summary>
 /// dispatched on devices when a user is about to be prompted to "install" a web application. Its associated event may be saved for later and used to prompt the user at a more suitable time.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnBeforeInstallPrompt(
     Func <BeforeInstallPromptEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(
                JsObjectRef, "",
                "beforeinstallprompt",
                CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                    async jsObjectRef =>
     {
         var beforeInstallPromptEvent = new BeforeInstallPromptEvent
         {
             Platforms = await JsRuntime.GetInstanceProperty <string[]>(jsObjectRef, "platforms")
         };
         beforeInstallPromptEvent.SetJsRuntime(JsRuntime, jsObjectRef);
         await callback.Invoke(beforeInstallPromptEvent);
     },
                    getJsObjectRef: true,
                    serializationSpec: false
                    )
                ));
 }
コード例 #12
0
        /// <summary>
        ///  listen for dispatched messages send by PostMessage
        /// </summary>
        /// <param name="todo"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task <IAsyncDisposable> OnMessage <T>(Func <OnMessageEventPayload <T>, Task> todo)
        {
            return(await jsRuntime.AddEventListener(
                       this.JsRuntimeObjectRef,
                       "",
                       "message",
                       CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                           async payload =>
            {
                var eventPayload = new OnMessageEventPayload <T>()
                {
                    Data = await jsRuntime.GetInstancePropertyAsync <T>(payload, "data"),
                    Origin = await jsRuntime.GetInstancePropertyAsync <string>(payload, "origin"),
                    Source = await jsRuntime.GetInstancePropertyAsync <WindowInterop>(payload, "source", false)
                };
                eventPayload.Source.SetJsRuntime(jsRuntime, await jsRuntime.GetInstancePropertyRefAsync(payload, "source"));

                await todo.Invoke(eventPayload);
            },
                           getJsObjectRef: true
                           )));
        }
コード例 #13
0
 /// <summary>
 /// Fired when the visual viewport is resized.
 /// </summary>
 /// <param name="todo"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnResize(Func <ValueTask> todo)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "resize", CallBackInteropWrapper.Create(todo)).ConfigureAwait(false));
 }
コード例 #14
0
 /// <summary>
 /// Fired when the visual viewport is scrolled.
 /// </summary>
 /// <param name="todo"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnScroll(Func <ValueTask> todo)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "scroll", CallBackInteropWrapper.Create(todo)));
 }
コード例 #15
0
 /// <summary>
 /// The languagechange event is fired at the global scope object when the user's preferred language changes.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async Task <IAsyncDisposable> OnLanguageCHange(Func <Task> callback)
 {
     return(await jsRuntime.AddEventListener(JsRuntimeObjectRef, "", "languagechange", CallBackInteropWrapper.Create(callback, getDeepObject: false)));
 }
コード例 #16
0
 /// <summary>
 /// The unload event is fired when the document or a child resource is being unloaded.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnUnload(Func <ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "unload",
                                             CallBackInteropWrapper.Create(callback, false)));
 }
コード例 #17
0
 /// <summary>
 /// A popstate event is dispatched to the window every time the active history entry changes between two history entries for the same document. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnPopState <T>(Func <T, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "popstate",
                                             CallBackInteropWrapper.Create <PopStateEvent <T> >(async p => await callback(p.State), new { state = "*" })));
 }
コード例 #18
0
 /// <summary>
 /// The scroll event fires when the document view or an element has been scrolled, whether by the user, a Web API, or the user agent.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnWheel(Func <WheelEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "wheel",
                                             CallBackInteropWrapper.Create(callback,
                                                                           new { deltaX = true, deltaY = true, deltaZ = true, deltaMode = true })));
 }
コード例 #19
0
 /// <summary>
 /// The scroll event fires when the document view or an element has been scrolled, whether by the user, a Web API, or the user agent.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnScroll(Func <ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "scroll",
                                             CallBackInteropWrapper.Create(callback, false)).ConfigureAwait(false));
 }
コード例 #20
0
 /// <summary>
 /// The pageshow event is sent to a Window when the browser displays the window's document due to navigation
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnPageShow(Func <PageTransitionEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "pageshow",
                                             CallBackInteropWrapper.Create(callback, new { persisted = true })));
 }
コード例 #21
0
 /// <summary>
 /// Fired when the visual viewport is scrolled.
 /// </summary>
 /// <param name="todo"></param>
 /// <returns></returns>
 public async Task <IAsyncDisposable> OnScroll(Func <Task> todo)
 {
     return(await this.jsRuntime.AddEventListener(this.windowRef, "visualViewport", "scroll", CallBackInteropWrapper.Create(todo)));
 }
コード例 #22
0
        /// <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 Task <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);

            return(new ActionAsyncDisposable(async() => await jsRuntime.InvokeVoidAsync("browserInterop.removeEventListener", jsRuntimeObject, propertyName, eventName, listenerId)));
        }
コード例 #23
0
 /// <summary>
 /// Tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.
 /// </summary>
 /// <param name="callback">ells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.</param>
 /// <returns>The request ID, can be used for cancelling it</returns>
 public async ValueTask <int> RequestAnimationFrame(Func <double, ValueTask> callback)
 {
     return(await JsRuntime.InvokeInstanceMethod <int>(JsObjectRef, "requestAnimationFrame",
                                                       CallBackInteropWrapper.Create(callback)));
 }
コード例 #24
0
 /// <summary>
 /// The orientationchange event is fired when the orientation of the device has changed.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async ValueTask <IAsyncDisposable> OnOrientationChange(Func <ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "orientationchange",
                                             CallBackInteropWrapper.Create(callback, false)));
 }
コード例 #25
0
 /// <summary>
 /// Called when the page is installed as a webapp.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 public async Task <IAsyncDisposable> OnAppInstalled(Func <Task> callback)
 {
     return(await jsRuntime.AddEventListener(JsRuntimeObjectRef, "", "appinstalled", CallBackInteropWrapper.Create(callback, getDeepObject: false)));
 }