تحتوي فئة WebView على عضوين قويين، وهما طريقة ExecuteScript() وحدث WebMessageReceived. يمكنك استخدامهما لإنشاء خوارزميات تصل إلى صفحة ويب من اختيارك (على سبيل المثال، موجز أخبار الفوركس أو أي صفحة بـ HTML مخصص)، وتنفيذ JavaScript مخصص على هذه الصفحة، ومعالجة أي رسائل مرسلة من WebView مرة أخرى إلى الخوارزمية بشكل صحيح.
تنفيذ البرامج النصية
لتنفيذ برنامج نصي مخصص داخل WebView الخاص بك، كل ما عليك فعله هو استدعاء طريقة WebView.ExecuteScript(StringjavaScript) كما هو موضح أدناه.
لإرسال رسالة من WebView، كل ما عليك فعله هو استدعاء window.postMessage() من كود JavaScript داخل صفحة HTML. على سبيل المثال، يمكنك تمرير البرنامج النصي في المثال أدناه لحساب الفرق المئوي بين رقمين (مثل أسعار الدخول والخروج المحتملة لمركز).
لا تتم مشاركة السياق بين C# وJavaScript عند استدعاء طريقة ExecuteScript(). إذا كنت ترغب في تمرير متغير من نطاق C# إلى نطاق JavaScript، فسيتعين عليك استخدام التضمين النصي.
من الممكن الجمع بين ExecuteScript() وOnWebMessageReceived لوضع عناصر تحكم داخل صفحة تم فتحها عبر WebView والتي، عند التفاعل معها، ترسل رسائل مرة أخرى إلى cBot. في المثال أدناه، نستخدم هذه الوظيفة للسماح بالتداول مباشرة داخل WebView. لاحظ أن WebView يفتح صفحة HTML مخصصة بدلاً من مورد موجود.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingcAlgo.API;usingcAlgo.API.Collections;usingcAlgo.API.Indicators;usingcAlgo.API.Internals;usingSystem.Text.Json;namespacecAlgo.Robots{[Robot(AccessRights = AccessRights.None)]publicclassWebViewInteractionsExample:Robot{privateWebView_webView;protectedoverridevoidOnStart(){varwindow=newWindow();_webView=newWebView();_webView.Loaded+=WebView_Loaded;_webView.NavigationCompleted+=WebView_NavigationCompleted;_webView.WebMessageReceived+=WebView_WebMessageReceived;window.Child=_webView;window.Width=500;window.Height=500;window.Show();window.Closed+=args=>Stop();// stop cBot when window is closed}privatevoidWebView_WebMessageReceived(WebViewWebMessageReceivedEventArgsargs){Print("Message received from WebView: {0}",args.Message);vartradeMessage=JsonSerializer.Deserialize<TradeMessage>(args.Message);;vartradeType=tradeMessage.side=="buy"?TradeType.Buy:TradeType.Sell;ExecuteMarketOrder(tradeType,tradeMessage.symbolName,tradeMessage.volume);}privatevoidWebView_Loaded(WebViewLoadedEventArgsargs){args.WebView.NavigateToStringAsync(@" <script> function setSymbols(symbols) // called from the cBot { const symbolSelector = document.getElementById('symbolHtml'); symbolSelector.innerHTML = ''; symbols.forEach((symbol) => { symbolSelector.add(new Option(symbol)); }); } function trade(operation) { const symbolSelector = document.getElementById('symbolHtml'); const selectedSymbolName = symbolSelector.options[symbolSelector.selectedIndex].text; window.postMessage({ side: operation, volume: Number(document.getElementById('volumeHtml').value), symbolName: selectedSymbolName }); //send message to cBot } </script> <body bgcolor='white'> <div style='display:flex;flex-direction:column;width:300px'> <h1>This is HTML page</h1> <div>Symbol:</div> <select id='symbolHtml'></select> <span style='margin-top: 10px'>Volume:</span> <input id='volumeHtml' value='1000' /> <div style='margin-top:10px'> <input type='button' value='Buy' onclick='trade(`buy`)' /> <input type='button' value='Sell' onclick='trade(`sell`)' /> </div> </div> </body> ");}privatevoidWebView_NavigationCompleted(WebViewNavigationCompletedEventArgsobj){SendSymbolsToHtml();}privatevoidSendSymbolsToHtml(){varallSymbols="";foreach(varsymbolinSymbols){allSymbols=allSymbols+"'"+symbol+"',";}_webView.ExecuteScript("setSymbols(["+allSymbols+"]);");// invokes the corresponding function inside HTML}}classTradeMessage{publicstringside{get;set;}publicdoublevolume{get;set;}publicstringsymbolName{get;set;}}}
عبر WebView، يفتح cBot صفحة HTML مخصصة تحتوي على عناصر تحكم تسمح لنا بوضع أوامر جديدة. باستخدام طريقة SendSymbolsToHtml()، يملأ cBot قائمة منسدلة داخل HTML بقائمة جميع الرموز التي يمكن للحساب تداولها. أثناء تشغيل cBot، يمكن للمستخدم تحديد رمز، وتحديد حجم الأمر ثم وضع أمر جديد مباشرة داخل صفحة الويب دون التفاعل مع عناصر التحكم داخل cTrader.