How to invoke Javascript in .NET in WPF & WinRT? - javascript

Here is a use case. I need to:
Invoke JavaScript from .NET (C#) code
JavaScript won't be compile, it comes from a dynamic source
I need to pass .NET objects to that javascript code, preferable a dynamic object
I also need to receive objects back from this javascript code
I need this code to run in WPF (Windows 7) & Windows Store (WinRT)
So far I've only seen activation of Windows Script Engines (i.e. IActiveScript) or using open source project.
I would prefer to stay clear of Open Source project and would prefer to use IE JavaScript engine (Chakra). Windows Script Engine doesn't seems to work on WinRT. I can live with hosting a browser in my app as it is a UI app.
Code doesn't need to be identical in WPF & WinRT (I can write something to wrap the two implementation behind an common interface).
How can I do that?

Chakra does not expose a public hosting interface, so you won't be able to do what you want without hosting something like V8 inside your application.
Another option for the metro app would be to build the main app in JavaScript and call out to WinRT objects written in .NET for the majority of the work. Won't help you in WPF though.

Related

Deleting a file in JavaScript

I am developing a native android app using HTML/CSS/JavaScript. During the App flow, I need to permanently delete certain HTML files (stored locally). Can this be done?
I am a high school teacher trying to develop an educational app. I have basic knowledge of HTML/CSS and near zero knowledge of JavaScript!
modern broswer support javascript file operation but this file system is only in broswer and isolated from local files, refer https://developer.mozilla.org/en/docs/Web/API/File
if u do need delete file on android system, then u need your app provide a bridge to and call from js, run in app, in java code, delete the file
the code might look like
JsBradge.delete(file)
Javascript disigned as a web document script language, so it has many security essentials, such as absolute isolation in browser, which means no write acces to file system.
So, you have two options - first is create your own bridge to java, for example call prompt('your_file_name'), catch it in java and delete your_file_name.
Second - use any of mobile app framework, for example - Cordova.
Sorry, but no easy ways =(

Native Plugin For Windows 8 Using Cordova

I am creating a windows8 plugin in cordova but i want to be integarte native build like .winmd with my plugin so that through Winjs i m calling the functions of that library which is in c#. I had already added the reference and code into plugin folder and as well as in project. But still it throws an error WinRt Error : class is not register/JavaScript Error : class in not defined.
I am creating same plugin in WP but there is issue in windows8 plugin development using cordova winjs with native functionality. please help to resolve this issue, provide some samples , links etc.
Updated:
It seems that to reference a WinRT component from a Cordova project using the current VS Tools for Cordova release, you need to follow the instructions here:
https://msopentech.com/blog/2014/11/24/using-windows-runtime-component-with-cordova-project/
In particular, section 1 describes how to inform the Cordova build tools about your WinRT component(s), so that it can reference it/them in the AppX manifest, and include the component binaries in the output package.
Earlier notes:
Do you need to actually write it as a Cordova plug-in? Windows JS apps support really simply integration of custom WinRT components, which can be written in C++ or C#. You just have to create a new Windows Runtime Component project in your solution, define a public sealed class with the methods/properties you want to expose, and then add a reference from your JS app project to the WinRT component (right-click References, select Add Reference, and choose the component project).
That said, be wary of the performance implications of including a C# component (particularly if you need to load it during startup) and the memory usage implications of loading the CLR and all the .NET dependencies. Also remember that C# is not "native" so it isn't really a "native plugin" if you do that :-)
You also might get slightly better advice if you describe your goal (i.e. you need the native plugin for something where you're actually running against JS performance limitations - which is a fairly exceptional case).

Unexpected ReSharper warnings when editing a JavaScript file in a console app

I wrote a small web app using Nancy. I was originally using ASP.NET hosting, but I decided to try Nancy self hosting so I could deploy the app easily to any Windows machine. This involved replacing my ASP.NET Empty Web Application with a Console Application.
Pretty much everything is working well, but I've run into an issue where using certain window functions, such as alert() or escape(), in the JavaScript editor causes ReSharper warnings.
Here's how it looks in the editor:
I could always disable these warnings (or add window. before every call), but I'm wondering if there's anything I can do to have these functions recognized as if my project were an ASP.NET app.
Edit
If I return to my original project (the ASP.NET app) and type alert, here is what I see:
Could this mean that DHtml.js doesn't get loaded/included for console apps? Is there a way to make that happen?
When you create a web project, ReSharper adds some "hidden" js files to its cache that provide definitions for global objects, including the "Dhtml.js" file you mention. These files aren't added for console applications. There's no decent workaround here - ReSharper's web support is primarily based on the idea that you're in a web project of some kind, so there will be other features that are not enabled due to the fact that you're in a console app project.
However, it looks like it's possible to replicate what ReSharper does to add these files from a plugin - instead of looking to see if the project is a web project, it should look to see if it's a console app that also references Nancy. If so, tell ReSharper to internally reference these "hidden" files. I'd suggest posting a feature request to the Nancy ReSharper plugin project.

How can I use a dll compiled with .Net Framework 4.0in a Windows Metro App

I am new to Windows 8 app development and professional C# dev. I have run into a problem where a client wants me to interface with a printer (DYMO 450 Turbo) and the SDK only comes with a dll that has been compiled using .net 4.0 . The samples show referencing the dll from the app which I can not do because the app is written in Javascript using WinJS. The metro apps use NETCore where the dll uses NETFramework. My best solution so far is to write the code to interface with the printer as a server in .NET 4.0 that listens for requests sent from Javascript over XMLHttpRequest. My gut tells me there HAS to be a better way, but either Ihavn't figured it out or maybe I just don't know enough to figure it out yet. Any help would be appreciated. Thanks in advance.
You cannot use a standard .NET 4.0 assembly in a Windows Store app. You can only use WinRT components. That said, your existing component may or may not be easy to convert to a WinRT componenet. If you simply include the project for your .NET 4.0 component and then change the Output Type (under the project properties) to Windows Runtime Component, you can have a look at all of the compiler errors that are thrown and get an idea of how big of a chore it's going to be to convert it. This article will let you know what the restrictions are on WinRT components.
Suggest add a project using project template c# - windows runtime component, to build a winrt component in c# that can use your sdk dll. winrt component can expose a wrapped impl. winrt component can be referenced in the winjs project.
I have used this for simple utils functions but not for integrating with third party sdk. suggest you give it a shot.

Creating NPAPI plugin in Delphi and accessing exported APIs using javascript

I have found a useful billards physics library in Borland delphi for my hobby project. The code is object oriented(in Object pascal). I want to visualize it using webGL (javascript) in chrome browser.
The idea is, I should have one call to make from the javascript which will access the interface from delphi lib and give me the new position, rotation and scale values in respective array.
For this I want to develop an NPAPI lib Scriptable plugin in Delphi(Meaning NO UI only lib of APIs).
1)I want to export those necessary physics APIs from the Delphi via this NPAPI plugin and access them in my html page using JavaScript. I am targeting chrome.
2) Any simple "hello world" tutorial or article related to this will be highly helpful.
3)Any help on using Firebreath to achieve the same is most preferable.
How to use Delphi with Firebreath?
use a good HTML / JavaScript editor (for example the NetBeans IDE) create a basic HTML page which contains the "hard-coded" JavaScript to display the initial scene, and a HTML form to accept user input
use Indy or Synapse to create a HTTP server (in Delphi), wich serves this HTML page and when the user submits form input, parse the form parameters and pass them to the billiard simulation, which should emit a list of graphic commands as a result - and convert these to WebGL (this is the hard part) statements
from the HTTP server, send a response (HTML document) back to the client, with the JavaScript / WebGL statements embedded, so that the client will see the animation and the new playfield state
repeat until end of game
Create a COM DLL and install it on the client's machine. Then Javascript can use its ActiveXObject class to access the COM object (or, under IE, you can use the <object> HTML tag). Make sure your COM object implements the IDispatch interface, as Javascript needs to use late binding since it is an interpretted environment and not compiled.

Categories

Resources