在Windows 8 / .Net 4.5中嵌入Chakra Javascript引擎

4

随着Javascript在Windows 8中的崛起,Windows 8 / .Net 4.5 / VS 2012是否提供了一种机制来在应用程序中嵌入Chakra JavaScript引擎以启用脚本编写?如果是这样,是否有相关文档可供参考?


根据您的需求,您可能可以使用另一个JavaScript实现,如Jint或IronJS。(Jurassic可能不起作用--它P / Invokes到V8,我认为在Metro应用程序中不允许P / Invoke。) - Joe White
请参阅https://dev59.com/pGw05IYBdhLWcg3wkig-。 - Cheeso
3个回答

1

目前还没有发布或讨论过实现这一机制的方法。目前,它仅在IE和Metro样式应用程序中可用。甚至连类似于Windows脚本宿主的曝光都没有。

你想要在你的脚本编写中使用哪个 Chakra 特性?


嗨Steve,就我的情况而言,我需要从Windows Store App和WPF中调用JavaScript。我们正在为Windows 8开发一个应用程序,以及它的前身WPF。我需要使用JavaScript作为脚本语言。因此,一方面,我有一个表示脚本的字符串和一个.NET对象模型。我需要将该对象模型传递给脚本,以便脚本在其上运行。您是否知道在Win Store App和传统.NET中实现这一点的任何方法?它可以是两种不同的机制。 - Vincent-Philippe Lauzon

0

是的,存在。

请参考:https://github.com/Microsoft/ChakraCore/wiki/Embedding-ChakraCore

 using System;
 using System.Runtime.InteropServices;
 using ChakraHost.Hosting;

public class HelloWorld
{
    static void Main() {
    JavaScriptRuntime runtime;
    JavaScriptContext context;
    JavaScriptSourceContext currentSourceContext = JavaScriptSourceContext.FromIntPtr(IntPtr.Zero);
    JavaScriptValue result;

    // Your script, try replace the basic hello world with something else
    string script = "(()=>{return \'Hello world!\';})()";

    // Create a runtime. 
    Native.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null, out runtime);

    // Create an execution context. 
    Native.JsCreateContext(runtime, out context);

    // Now set the execution context as being the current one on this thread.
    Native.JsSetCurrentContext(context);

    // Run the script.
    Native.JsRunScript(script, currentSourceContext++, "", out result);

    // Convert your script result to String in JavaScript; redundant if your script returns a String
    JavaScriptValue resultJSString;
    Native.JsConvertValueToString(result, out resultJSString);

    // Project script result in JS back to C#.
    IntPtr resultPtr;
    UIntPtr stringLength;
    Native.JsStringToPointer(resultJSString, out resultPtr, out stringLength);

    string resultString = Marshal.PtrToStringUni(resultPtr);
    Console.WriteLine(resultString);
    Console.ReadLine();

    // Dispose runtime
    Native.JsSetCurrentContext(JavaScriptContext.Invalid);
    Native.JsDisposeRuntime(runtime);
}

}


1
请考虑在StackOverflow添加代码示例,因为链接可能会失效或存储库可能被删除。 - lloyd

0

IE ActiveX难道不使用与IE独立版相同的JavaScript引擎吗?

您可以简单地嵌入Internet Explorer ActiveX框架并将其隐藏。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接