I am writing a WinRT application in C#.
I would like to execute some Javascript code. An entire WebView is not needed in my case, I just need to interface with a Javascript virtual machine. The C# code would call Javascript functions and would export some objects to the Javascript side.
I know there is JsRT, the C API of Chakra, the jsvm used by Internet Explorer. But JsRT, as far as I know, is not available to WinRT. Other virtual machines are not allowed under WinRT.
So, the question is: is it even possible to call Javascript code from C# code? If not, waiting for the release of JsRT for WinRT is the only option?
It's possible to call JavaScript from C# but not without a WebView control.
Related
I'm trying to build a .NET core application that execute JavaScript, this JavaScript also need to call C# functions on CLR objects as well ,I used JINT (https://github.com/sebastienros/jint) but it has one majeure disadvantage which i couldn't debug any JavaScript code.
So thinking for alternative that using CEF sharp that will run Blazor to execute javascript alongside C#. is it possible ?
Thanks!
You can use JSInterop in blazor to communicate between JS and C#.
Read more here for calling JS from C#:
https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-5.0
And here for calling C# from JS:
https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript?view=aspnetcore-5.0
Webassembly can compile say C# to bytecode, executable by the browser.What is its rendered form in the browser? HTML with JavaScript? Or something like Silverlight or a Java applet that runs inside the browser such as a business app with rich GUIs?
Your application code written in C# is compiled into asp.net assembly, and is managed by the mono run time, which was compiled to WebAssembly. What is rendered in the browser is html through manipulation of the DOM using JavaScript interop; that is, your C# code communicate with JavaScript code to manipulate the DOM, and rerender the diffs.
In simple terms WebAssembly is a lightweight virtual machine that can executed numeric instructions. It cannot render HTML, or use any WebAPIs directly.
You cannot however import / export WebAssembly functions to allow it to communicate with JavaScript. Therefore WebAssembly apps tend to use DOM or canvas via Javascript bindings.
I'm trying to write a module of code which can be used with Javascript for the client side browser and Kotlin for a Java desktop application and Android app.
The main logic manipulates a bitmap/png file.
Is there a way which I can write an interface to be used and have different implementations of the interface for JS and Kotlin?
For example, write a wrapper class for an image (load from image, set pixels, get pixels) in Java (using BufferedImage) and JS (using Canvas)?
I'm new here so if anything doesn't make sense or needs more clarification, please let me know!
Yes, it's certainly possible to do that. This answer is a generic Java and JavaScript answer; there may also be a Kotlin-specific approach, see the link in zsmb13's comment. But the generic Java and JavaScript approach would be:
Write your interface around BufferedImage in Java
Write your interface around canvas in JavaScript
Write your manipulation code using that interface in JavaScript
On the browser, your manipulation code would run directly on the browser's JavaScript engine
In the Java app, you'd run your JavaScript code using javax.script to run it in Nashorn (or Rhino on older Java environments).
Whether it's the best solution to your problem is something only you can decide, but yes, you can do it.
I'm creating a video game that has single player and multiplayer. The single player is done in C# (Unity), and want to use Gamesparks BaaS that works on Javascript (NodeJS) for their server code.
Since I want the multiplayer server to be authoritative, I need the server code to run Javascript and the client will run C#, which means duplicated code
But, if I can create .NET dlls in Javascript I can reduce code duplication heavily by using those libraries in Unity and using the Javascript code on the server.
Thanks!
If you want to use javascript (and by javascript I mean nodejs), along with c# and communicate between those two, you can use electron-edge. It helps you to run c# code using nodejs through Electron API.
Here is the link of their Github url: Electron-Edge-documentation
You can also interact with dlls, using an creating your application as an electron app. You refer this question where you will find some more details regarding it.
I have VB.NET code that handles automation of various application installs. I want to move this essentially out of a VB generated EXE package, and be able to execute the same code (or the equivalent result) from an HTML page on a server. Is this possible? It looks like javascript can't cross the web application/desktop application barrier. Perhaps I can execute the VB.NET code/application that handles the automation (stored server side) from code in the HTML? This is a fairly broad question(s) so ideas are welcome. Please post examples with your ideas!
Thanks.
Ian's comment made me read the question again. If you really plan to execute code on the client through a web browser, please ignore this answer.
You can use any server-side language that you want to do this. Since you already wrote it in VB, you can use ASP.NET or PHP's exec(). Example:
<?php
if($_GET["dosomething"]) {
exec("mycommand");
}
will execute mycommand when you pass dosomething as a paramter to the file.