简体   繁体   English

我可以从Windows 10(UWP)应用程序中的Web Worker调用自定义运行时组件吗

[英]Can I call a custom runtime component from a web worker in a Windows 10 (UWP) app

We are planning to write an Universal Windows Platform app for Windows 10 in JavaScript. 我们计划使用JavaScript为Windows 10编写通用Windows平台应用程序。

We will need to write as well some custom runtime components, most likely in C#. 我们还需要编写一些自定义运行时组件,最有可能使用C#。

I know it is possible to call a custom runtime component from the main UI thread. 我知道可以从主UI线程调用自定义运行时组件。 Now the question is: 现在的问题是:

In is possible to call a custom runtime component from within a Web Worker ? Web Worker中可以调用自定义运行时组件吗?

Does it require some special configuration/settings/api? 是否需要一些特殊的配置/设置/ API?

Thanks! 谢谢! Juan 胡安

is possible to call a custom runtime component from within a Web Worker? 可以从Web Worker中调用自定义运行时组件?

I've made a basic Demo. 我做了一个基本的演示。 The custom runtime component works well for Web Worker. 定制运行时组件对于Web Worker来说效果很好。

What I've done: 我所做的:

  1. I created a custom runtime component and reference it in my js-based UWP Project. 我创建了一个自定义运行时组件,并在基于js的UWP项目中引用了它。 Then I revised the Class1.cs to Example.cs like below: 然后我将Class1.cs修改为Example.cs,如下所示:

     public sealed class Example { public static string GetAnswer() { return "This is Answer"; } } 
  2. In my js-based UWP Project I revised main.js like below: 在基于js的UWP项目中,我修改了main.js,如下所示:

     // Your code here! (function () { "use strict" var w; if (typeof Worker !== "undefined") { var w = new Worker("ms-appx:///js/webWorkerDemo.js"); w.onmessage = function (evt) { document.getElementById("myContent").innerText = evt.data; w.terminate(); } } })(); 

    and put myContent div in index.html: 并将myContent div放在index.html中:

     <body> <div id="myContent"></div> <script src="js/main.js"></script> </body> 
  3. I added webWorkerDemo.js under js folder, which did nothing but call the runtime component's GetAnswer method and postMessage: 我在js文件夹下添加了webWorkerDemo.js,它什么都没做,只是调用了运行时组件的GetAnswer方法和postMessage:

      var str = MyRuntimeComponent.Example.getAnswer(); postMessage(str); 

When the app runs, "This is Answer" shows correctly in the app. 当应用运行时,在应用中正确显示“这就是答案”。 So it should be no problem when using custom runtime component within a Web Worker. 因此,在Web Worker中使用自定义运行时组件时应该没有问题。

And Here is the complete Sample: WinJSSample . 这是完整的示例: WinJSSample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM