.NET 4.x Внутри программы на C# выполнить JS код и передать результаты в основной код C#
Формулировка задачи:
Добрый день. Требуется внутри программы на C# выполнить JS код и передать результаты в основной код C# , где с ними будут производиться дальнейшие манипуляции. Подскажите, как это можно реализовать?
Использовал так на C#
Но получаю такой ответ
Листинг программы
- var now = new Date();
- var then = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
- then += ' '
- if (now.getHours() < 10) {
- then += '0' + now.getHours();
- }
- else {
- then += now.getHours();
- }
- then += ':';
- if (now.getMinutes() < 10) {
- then += '0' + now.getMinutes();
- }
- else {
- then += now.getMinutes();
- }
- console.Print(message + ' time(' + then + ')');
- console.Print(message + ' toffset(' + temp + ')');
- var L = navigator.plugins.length;
- var T = L;
- for (var i = 0; i < L; i++)
- {
- T = T + ' ' + navigator.plugins[i].name + ' ' + navigator.plugins[i].filename
- }
- console.Print(message + ' tplugc(' + L + ')');
- console.Print(message + ' tplug(' + T + ')');
- console.Print(message + ' tbuild(' + navigator.productSub + ')');
- var canvas = document.createElement('canvas');
- var ctx = canvas.getContext('2d');
- var txt = 'http://www.plentyoffish.com';
- ctx.textBaseline = 'top';
- ctx.font = '14px 'Arial'';
- ctx.textBaseline = 'alphabetic';
- ctx.fillStyle = '#f60';
- ctx.fillRect(125, 1, 62, 20);
- ctx.fillStyle = '#069';
- ctx.fillText(txt, 2, 15);
- ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
- ctx.fillText(txt, 4, 17);
- console.Print(message + ' tcrc(' + canvas.toDataURL() + ')');
Листинг программы
- using Noesis.Javascript;
- ....
- using (JavascriptContext context = new JavascriptContext())
- { // Setting external parameters for the context
- context.SetParameter("console", new SystemConsole());
- context.SetParameter("message", "Hello World !");
- context.SetParameter("number", 1);
- // Script
- String script = @"
- var now = new Date();
- var then = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
- then += ' '
- if (now.getHours() < 10) {
- then += '0' + now.getHours();
- }
- else {
- then += now.getHours();
- }
- then += ':';
- if (now.getMinutes() < 10) {
- then += '0' + now.getMinutes();
- }
- else {
- then += now.getMinutes();
- }
- console.Print(message + ' time(' + then + ')');
- console.Print(message + ' toffset(' + temp + ')');
- var L = navigator.plugins.length;
- var T = L;
- for (var i = 0; i < L; i++)
- {
- T = T + ' ' + navigator.plugins[i].name + ' ' + navigator.plugins[i].filename
- }
- console.Print(message + ' tplugc(' + L + ')');
- console.Print(message + ' tplug(' + T + ')');
- console.Print(message + ' tbuild(' + navigator.productSub + ')');
- var canvas = document.createElement('canvas');
- var ctx = canvas.getContext('2d');
- var txt = 'http://www.plentyoffish.com';
- ctx.textBaseline = 'top';
- ctx.font = '14px 'Arial'';
- ctx.textBaseline = 'alphabetic';
- ctx.fillStyle = '#f60';
- ctx.fillRect(125, 1, 62, 20);
- ctx.fillStyle = '#069';
- ctx.fillText(txt, 2, 15);
- ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
- ctx.fillText(txt, 4, 17);
- console.Print(message + ' tcrc(' + canvas.toDataURL() + ')');
- ";
- // Running the script
- context.Run(script);
- // Getting a parameter
- Console.WriteLine("number: " + context.GetParameter("number"));
- }
Серьезность Код Описание Проект Файл Строка Состояние подавления
Ошибка CS0246 Не удалось найти тип или имя пространства имен "SystemConsole" (возможно, отсутствует директива using или ссылка на сборку). WindowsFormsApp2 c:\users\kurgan\documents\visual studio 2017\Projects\WindowsFormsApp2\WindowsFormsApp2\Form1.cs 56 Активный
Решение задачи: «.NET 4.x Внутри программы на C# выполнить JS код и передать результаты в основной код C#»
textual
Листинг программы
- var txt = 'http://www.plentyoffish.com';
- ctx.textBaseline = 'top';
- ctx.font = '14px 'Arial'';
- ctx.textBaseline = 'alphabetic';
- ctx.fillStyle = '#f60';
- ctx.fillRect(125, 1, 62, 20);
- ctx.fillStyle = '#069';
- ctx.fillText(txt, 2, 15);
- ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
- ctx.fillText(txt, 4, 17);
- //tcrc'+canvas.toDataURL()';'
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д