aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Web/Tango.MachineService.Gateway/App_Start/WebApiConfig.cs
blob: d3bc653da1695d8a124676c41c234dacbdb77eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Tango.PPC.Browser.Attributes;
using CefSharp;
using CefSharp.Wpf;
using Tango.Core.Helpers;
pan>public static class BoundObjectsHelper { private static DispatcherTimer _timer; private static Dispatcher _dispatcher; private static ChromiumWebBrowser _browser; private static List<String> _scripts = new List<string>(); public static void RegisterAllBoundObjects(ChromiumWebBrowser browser, Dispatcher dispatcher) { _dispatcher = dispatcher; _browser = browser; _timer = new DispatcherTimer(DispatcherPriority.Background, dispatcher); _timer.Tick += _timer_Tick; _timer.Interval = TimeSpan.FromSeconds(2); _timer.Stop(); foreach (var type in typeof(BoundObjectsHelper).Assembly.GetTypes().Where(x => x.GetCustomAttribute<BoundObjectAttribute>() != null)) { var att = type.GetCustomAttribute<BoundObjectAttribute>(); var script = EmbeddedResourceHelper.GetEmbeddedResourceText($"Tango.PPC.Browser.Scripts.{att.ScriptFile}"); _scripts.Add(script); browser.JavascriptObjectRepository.Register(att.Name, Activator.CreateInstance(type), true); browser.FrameLoadEnd += Browser_FrameLoadEnd; } } private static void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e) { _timer.Stop(); _timer.Start(); } private static void _timer_Tick(object sender, EventArgs e) { try { _timer.Stop(); _dispatcher.BeginInvoke(new Action(() => { foreach (var script in _scripts) { _browser.GetMainFrame().ExecuteJavaScriptAsync(script); } })); } catch { _timer.Start(); } } } }