Calling C# component from JavaScript in Windows 10 Universal App - javascript

I'm trying to use Windows Runtime Component (C#) in my Windows 10 Universal App (JavaScript).
I found how to do that in Windows 8.x store apps:
https://msdn.microsoft.com/en-us/library/hh779077.aspx
but this solution is not working with Windows 10 Universal App. It is throwing exception that class is not registered in the JavaScript.
WRC code:
namespace SampleComponent
{
public sealed class Example
{
public static string GetAnswer()
{
return "The answer is 42.";
}
public int SampleProperty { get; set; }
}
}
In JS:
document.getElementById('output').innerHTML =
SampleComponent.Example.getAnswer();

The any CPU mode is not available (by default) if you develop a Windows 10 app written with XAML/C# or an app written with HTML/JS using a WinRT component written with C# because of the ".Net native"
You have to target the good platform :)

After some research I found that this problem occurs when building in Any CPU mode. For x86/x64 it is working properly. This solution is enough for me at this moment. I will post more information here if I found how to run it on other platforms.

Related

what about JavaScript in android?

I am a android developer. I want to develop an android app but want to code in javascript for that. Is it possible using ReactNative?
http://www.reactnative.com/
You can defiantly write phone apps in JS, there are quite few options on the market:
On one hand there are hybrid apps, which written in HTML, CSS, JS and are built usually by something like cordova in order to communicate with phone API's. Some good frameworks that help you with the build and styling proccess are: Phonegap and Ionic.
And you have the react-native approach which basicly compiles the JS code to native phone components.
Both approaches let you reuse parts of your code in multiple platforms(Android, IOS).
While in hybrid apps you can reuse almost all of your code but just build for each platform. On react native you will have to code your views for each platform while your BL will stay the same if you written the code properly.
You have to use WebView for this. Then register the JavaScriptInterface on your webview. JavaScriptInterFace can be a inner class as shown below. This class will have a function that you can call from html page( via javaScript ) and inside this function you can write code to change activity.
public class JavascriptInterfaceActivity extends Activity {
/** Called when the activity is first created. */
WebView wv;
JavaScriptInterface JSInterface;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView)findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
// register class containing methods to be exposed to JavaScript
JSInterface = new JavaScriptInterface(this);
wv.addJavascriptInterface(JSInterface, "JSInterface");
wv.loadUrl("file:///android_asset/myPage.html");
}
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
public void changeActivity()
{
Intent i = new Intent(JavascriptInterfaceActivity.this, nextActivity.class);
startActivity(i);
finish();
}
}
}
Here is the html page
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
JSInterface.changeActivity();
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
Yes obviously you can use ReactNative to develop the android applications. You need to write the app code in javascript in this case.
You must use react native in this case. It has native components exposed to javascript. If you need some special component(That is not available in GitHub), you can write that native module in Android and IOS respectively. Also, it has a great community support.

How to access C# dll using JavaScript

i have a class library in c# and i added that dll as a reference to the web site and I want to access the methods which are in the class library.
when I tried accessing those methods using ActiveXobject it throwing an exception saying object cant be created
namespace Office
{
public class Algebra
{
public double Addition(double x, double y)
{
return x + y;
}
}
}
this is my method in c# class library.
and my javascript is as follows
(function () {
alert("suresh");
var myobj;
myobj = new ActiveXObject("Office.Algebra");
alert(myobj);
var add = myobj.Addition(7, 6);
alert(add);
})();
No! Don't go this way.
If you want to access server resources from the client-side, you'll need to design and implement a Web service, for example on top of REST principles, which drives you directly to ASP.NET Web API.
In summary: expose resources from the backend using a Web service and access them using AJAX.
If I'm not mistaken in guessing, you have a dll that you want to run on the web browser (iirc, only IE). If that's the case, then you have to register the dll on the client computer ( with regsvr, regasm, depending on your dll).
But I agree with Matias, use it as last resort, for example, you have to call this dll for some legacy hardware requirement, legacy app, etc, and you've spent 3 months at least looking for another way. :p

can we call an android activity from Phonegap project?

I'm Developing an android application.
I've created some codings using HTML5 and javascript for android. And created a signature capturing application in Native JAva
I wanted to include the Native Signature capture coding in the HTML5 application. Because the signature capturing in the HTML5 is not working perfect for touch devices.
is it possible to call a native android activity from the HTML5 phonegap android application????
Yes, it is possible.
For doing the same you need to add cordova lib to your android project and moreover create a plugin in android by extending the Plugin class.
The overrided method named execute() is there that you can call from your javascripts.
Remember you need to define the path of the class containing execute folder to the config.xml.
Please visit below for complete guide.
Android Plugin
Any Java Native code call be called without using any plugin as following.
Follow The following Steps.
1) Replace the following code with your existing DroidGap Activity.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init(); // Calling this is necessary to make this work
appView.addJavascriptInterface(this, "MainActivity");
/* "this" points the to the object of the current activity. "MainActivity" is used to refer "this" object in JavaScript as in Step 3. */
super.loadUrl("file:///android_asset/www/index.html");
}
2 Add the custom function in current (this) activity as following.
public void customFunctionCalled() {
Log.e("Custom Function Called", "Custom Function Called");
}
3 Now call this function from your HTML/JavaScript code as following.
<script type="text/javascript">
function callNewActivity() {
window.MainActivity.customFunctionCalled();
}

#JavascriptInterface cannot be resolved

I'm working with Android to make a webpage interact with my app. As such, I've gone through the documentation and tutorials and ended up coming up with this site. In it, the developers list that you should include #JavascriptInterface before any function you wish to be accessible by the WebView and that without it, Jelly Bean won't recognize the function.
My problem is that when I put that in, I get an error saying:
#JavascriptInterface cannot be resolved to a type
Without it, my code compiles and works fine, but I want Jelly Bean compatibility. I'm currently working on Android 1.6 as a base, so does it just not have #JavascriptInterface? Is that a Jelly Bean specific thing, meaning I'll have to make a program specifically for Jelly Bean? Any help would be greatly appreciated. Here is my complete interface class:
import android.content.Context;
public class WebAppInterface{
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
//needed for jelly bean
#JavascriptInterface
public void turnOff() {
MyAlarmPopup.turnOff();
}
}
#JavascriptInterface is introduced in JellyBean.
You need to set the Project Build Target to API Level 17
From Eclipse IDE, go to
Project->Properties.
Select Android and set the target
The APK will work on older versions of android.
You need to add this:
import android.webkit.JavascriptInterface;
Check the sample here:
Steps for users of Android Studio:
Check that in build.gradle the minSdkVersion is 17 or above
Add import android.webkit.JavascriptInterface;

Run custom javascript code after loading any website

I am working on taking readings about web browser performance and so need to access the window.performance object of the browser.
To collect this data i have written a javascript file, collect.js which i need to add to the DOM of the page that i need to test eg. www.google.com, www.facebook.com and so on...
Also i need to run this test for about 1000 websites, any manual approach is out of the question. I need it to be automated somehow.
How could i go about doing this?
EDIT: I need to run these tests on an android browser, so i need mobile oriented solutions.
You can create a simple android app with a WebView component. This way you can control which URLs are loaded and also insert your JS code.
http://developer.android.com/guide/tutorials/views/hello-webview.html
EDIT
You can run any javascript like this:
Implement a custom WebView:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// Execute your javascript below
view.loadUrl("javascript:...");
}
}
If you are looking for an automated solution, try PhantomJs this provides an automated headless web browser. Also has access to network traffic
perhaps you can try "bookmarklet"
http://www.bookmarklets.com/
the advantage over greasemonkey script is that it can run on
firefox and explorer

Categories

Resources