I am building a phonegap like solution using MonoTouch & UIWebView. I would like to expose a number of C# methods via Javascript. I know you can do this in Objective C using WebScriptObject but I can't see where/if this is exposed in the MonoTouch API's.
Is there a way to access WebScriptObject?
Is there a way to access WebScriptObject?
No, WebScriptObject is not part of MonoTouch (like it is part in MonoMac) since WebKit, as a framework, is not part of iOS.
UIWebView does allow you to evaluate JavaScript code but I'm not sure (never tried or even looked for) how you can, from there, get back into C# methods.
Just as as corollary to Sebastian's answer you can always create a simple binding that allows your UIWebView to interact with C# code via a HttpListener:
http://yobriefca.se/blog/2011/12/05/rolling-your-own-phonegap-with-monotouch/
You can also use iOS Background threads to interact with other apps via Javascript or C# using HttpListener:
http://blog.devnos.com/remember-when-apps-didnt-talk-to-each-other
As some additional side answers to this...
you can access javascriptcore - but you will have to work out the bindings for this
some people have managed to use Jint (0.9 or older - with no Reflection.Emit JIT code)
Related
Is it possible to write code in Xamarin (C#) and then compile it into a library (.a) that I can use in Objective-C/Swift?
Then, if I could do the same for Java/Adroid, I could write the logic in common C# and the rest of the app in their native environment.
No.
The Xamarin bindings only work one-way - Xamarin can consume native code, but native code cannot consume Xamarin.
This may not be answer op wants, but... there are frameworks that compile C# code into Javascript (that despicable loosely typed) but ubiquitous language that we should all learn well (despite our rejecting it since Netscape 1 because we are strong type bigots).
Alas we see with HTML5 that even MSFT is abandoning their traditional development platforms in favor of (yes, get this, Javascript). Tough pill to swallow however, we see the open community has done way more in arena of plug-ins to Javascript than MSFT ever did with C#. C# is powerful, solid, mature, fantastic, but it is not the webbrowser language of choice.
MSFT developed typescript for a reason, we see JQuery which brought name-type addressibility, alas the stage is set for Javascript, Java and C# to either play nice together or get of the "World Stage" Studying and knowing Javascript, Jquery et. al. is a good thing to do.
We have C# code, which is backend for us. Now we need it to be used in our website which is developed in Drupal.
As far as I know, there is no easy way to do it. You basically have 2 options:
Rewrite the code in php. It may be not possible to do it however, it depends on what the code does.
Expose c# code as an external webservice (e.g. with WCF) and then consume the webservice. Drupal is able to do it - more info.
A sloppy solution would also be to display your C# page in the Drupal page with an iframe. I do also agree with empi's answer to rewrite the code in php or a web service depending on your requirement.
You can build DLL project (class library) in C# and use the DLL as reference back in the drupal project.
Not sure about this, and never done before.
You better research more.
But, the best way to use the .NET code in another application is to build the required code into DLL project and use in whichever language you want to use.
We've written a Java program which we are looking to use and interact with from C#. What are our options? Optimally it would be possible to compile the Java application as a library (.DLL) that we could reference from C# perhaps using P/Invoke. This, however, doesn't appear to be an option according to the first few searches online.
We opt to be able to use ASP.NET to built a search engine powered by the Java code, so if this opens up for any other options please let us know.
Sorry, you cannot call java code / classes Directly from C# code.
One way of doing this is to wrap up your java classes in a java Web Service and call classes indirectly through that web service interface in your C# code.
Another way is using
javareg.exe which exposes java classes as COM. You can find it at following location:
C:\Program Files\Microsoft VisualStudio\VIntDev98\bin\javareg.exe
Following posts might help as well
Calling Java Classes Directly from
.NET (uses runtime bridge)
Calling Java from Microsoft.NET
The simplest approach would probably be to publish the functionality of your java library as web services and add a web-reference from your asp.net application.
Java isn't meant to be embedded in another program, so you need a bridge. The most simple solution is to use a socket: Create a Java process which listens for commands on a socket. In the C#, send the commands to the socket and read the answers.
The main problem here is serialization but if you use XML, it's not such a big pain anymore. Try the built-in XML serialization (see this article) or custom frameworks like XStream or Simple.
It is certainly possible to wrap Java in a .dll, and has been a part of the core Java platform for over 10 years. JNI (Java Native Interface) has an interface for embedding a JVM in your code, meaning you can run Java classes using C-style linking. Note that this will require that you write a simple C wrapper, there are samples within:
http://java.sun.com/docs/books/jni/html/invoke.html#11202
As some of these other posts suggest, sometimes it's desirable to be less tightly coupled, so you may want to consider using another design. One option would be a simple database, where the Java application regularly polls for requests from the C# code. If you want tighter coupling, for things like call-backs, you can look at distributed interfaces.
Currently I'm involved in a project using c# . I know some of the features of this project can be easily implemented using Javascript (I have been a js developer for some time, but totally new to c#). so I'm very eager to know if it is possible to reuse the available js code in the project, or put it another way, embeded js into c# .
thanks in advance.
I think you're asking if it is possible to run JavaScript on the CLR. Similar to Java 6's JVM script engine.
EcmaScript.net could be your best bet - http://code.google.com/p/ecmascript-net/ - see also this question - Are there any .NET CLR/DLR implementations of ECMAScript?
There is an extensive article written on how to embed Javascript within C# here. Is that what you are looking for?
Are you referring to ClientScriptManager RegisterClientScriptBlock Method
There is no reason you cant continue to use existing client side javascript code.
There is a still a clear distinction to whats required on the client and what can be called by the client.
Various possibilities include: calling web services, making ajax calls to WebMethods and injecting javascript code into pages.
The way you write it it sounds like a giant cop-out of learning C# properly. Unless you actually mean that you want to use JavaScript for client-side control of your project, this is a no-go (and it doesn’t sounds like you meant client-side scripting).
That said, .NET actually allows you to mix assemblies from different languages effortlessly, but this only applies to .NET languages which can be compiled to .NET IL (bytecode) and I’m not aware of a JS-to-IL-compiler.
Javascript runs in the browser, as long as it is a web project there is no problblem combining. C# code will run on the server, javascript code in the browser.
http://msdn.microsoft.com/en-us/library/aa479011.aspx
I'd like to host Javascript in my C# program. I would like to allow users to write custom Javascript code, and have my C# program run their functions, as well as allow the users to use my framework code.
Is it possible to do this? If so, how?
Edit: To be clear, I am not using ASP.NET for this project.
you can also use a webbrowser control to host the javascript in a html document, to interact between the two you would make a COM visible class and set an instance of it to the ObjectForScripting property.
Any public members of the 'external' class are now accessible through window.external in javascript. Also, from the managed code side you can use the InvokeScript method of the document to call any javascript functions.
This way it is easy to pass complex objects between the two.
I used this idea quite a bit in this Google Earth Api application.
http://fraserchapman.blogspot.com/2008/08/google-earth-plug-in-and-c.html
I'd highly recommend you use the Microsoft Dynamic Langauage Runtime (DLR). It's purpose in life is to facilitate scripting in a .Net environment. The DLR is not script specific so over time more script languages will be built on top of it but at the moment the ones I know about are IronRuby, IronPython and JScript.
Spidermonkey .NET Is probably what you are looking for.
I'm not sure how developed it is. Buts its basically a port of Mozilla's Spidermonkey Javascript parser engine which I like a lot.
Or you can evaluate it directly
The way to do this used to be the Windows Script Host, which permitted you to host either JSCript or VBScript.
You can certainly run Javascript natively from C# - see this article for a starter. Having the Javascript access your framework could be tricker, though. Perhaps Script# could help?
There are HTML components where you can embed JavaScript. For the other way the proper thing is an ASP.NET web service. Perhaps with a smart client you can combine both.