WinRT 上的 C++、C# 和 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7466303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++, C# and JavaScript on WinRT
提问by Anonymous
From image below, Windows 8 Platform and tools. I know this mean I can use C++, C# or JavaScript for Metro style App. I also watch some build's keynote and I have couple of questions here.
从下图中,Windows 8 平台和工具。我知道这意味着我可以将 C++、C# 或 JavaScript 用于 Metro 风格的应用程序。我还观看了一些构建的主题演讲,我在这里有几个问题。
Windows 8 平台和工具 http://www.windowsitpro.com/content/content/140554/windows8-platform-tools_2.jpg
- Do they have any difference in C++, C# and JavaScript on WinRT, e.g. performance, feature, capability etc.
- How can I create nativeMetro App using JavaScript, do I need to use js library from MS or I can use whatever js that I familiar with, for example jQuery.
- In Metro style App, System Services is only WinRT, does this mean I can't use low-level dll anymore? Will this come with performance cost?
- 它们在 WinRT 上的 C++、C# 和 JavaScript 中是否有任何差异,例如性能、特性、功能等?
- 如何使用 JavaScript创建本机Metro App,我是否需要使用 MS 的 js 库,或者我可以使用我熟悉的任何 js,例如 jQuery。
- 在 Metro 风格的 App 中,系统服务只有 WinRT,这是否意味着我不能再使用低级 dll 了?这会带来性能成本吗?
回答by Pavel Minaev
Regarding #1, the line-up would be roughly as follows:
关于#1,阵容大致如下:
JavaScript - highest-level, dynamically typed, GC. You can only use HTML5/CSS for your UI, the XAML framework (Windows.UI.XAML
namespace) is not available. Provides some standard JS APIs (specified by HTML5) in addition to the available surface of WinRT, such as local storage or IndexedDB. Being dynamically typed, heavy CPU-bound processing is likely to be slower than either .NET or C++, though the JS engine is still very fast due to being JIT-compiled and heavily optimized. You can consume C++ and .NET WinRT components, but not write your own in JS. Some aspects of language projection seem to be limited correspondingly - e.g. so far as I can see, there's no way to implement a WinRT interface in JS, for example. Existing JS libraries can usually be reused with no or minimal effort, so long as they work in IE10.
JavaScript - 最高级别的、动态类型的、GC。您只能将 HTML5/CSS 用于您的 UI,XAML 框架(Windows.UI.XAML
命名空间)不可用。除了 WinRT 的可用表面外,还提供一些标准的 JS API(由 HTML5 指定),例如本地存储或 IndexedDB。由于是动态类型的,大量 CPU 密集型处理可能比 .NET 或 C++ 慢,尽管 JS 引擎由于经过 JIT 编译和大量优化而仍然非常快。您可以使用 C++ 和 .NET WinRT 组件,但不能在 JS 中编写自己的组件。语言投影的某些方面似乎相应地受到限制 - 例如,就我所见,例如,无法在 JS 中实现 WinRT 接口。现有的 JS 库通常可以不费吹灰之力地重用,只要它们在 IE10 中工作即可。
.NET (C#/VB) - mid-level, statically typed with optional dynamic typing (dynamic
keyword etc) and GC. XAML UI framework is the default one for UI, but you can also use HTML by using WebView
control. Provides full access to WinRT libraries, but also some of its own on top of that, which are sometimes more convenient to use (e.g. Stream
vs IInputStream
/IOutputStream
). Also, the only one that includes special language-level support for asynchronous operations (async
and await
keywords), which are used heavily when using WinRT APIs due to their highly asynchronous design. Generally speaking, provides most syntactic sugar - aside from async stuff, you get LINQ to objects (which works over WinRT collections). Can write your own WinRT components, which can then be used from JS or C++/CX. Existing .NET libraries may or may not be readily reusable, depending on what classes in .NET Framework they rely on; components written for Silverlight or WP7 are most likely to be reusable with no or minimal changes, while components written for .NET 4 Full or Client Profile may require significant changes to run.
.NET (C#/VB) - 中级,静态类型,可选动态类型(dynamic
关键字等)和 GC。XAML UI 框架是 UI 的默认框架,但您也可以通过使用WebView
控件来使用 HTML 。提供对 WinRT 库的完全访问,但也提供了一些它自己的库,有时使用起来更方便(例如Stream
vs IInputStream
/ IOutputStream
)。此外,唯一一个包含对异步操作的特殊语言级支持(async
和await
关键字),由于它们的高度异步设计,在使用 WinRT API 时大量使用。一般来说,提供最多的语法糖 - 除了异步的东西,你得到 LINQ to objects(它在 WinRT 集合上工作)。可以编写自己的 WinRT 组件,然后可以从 JS 或 C++/CX 中使用。现有的 .NET 库可能容易重用,也可能不容易重用,这取决于它们所依赖的 .NET Framework 中的哪些类;为 Silverlight 或 WP7 编写的组件最有可能无需更改或只需少量更改即可重用,而为 .NET 4 Full 或 Client Profile 编写的组件可能需要进行重大更改才能运行。
C++/CX (Visual C++ Component Extensions) - low/mid-level, statically typed, no GC - refcounting only. Closest "to the metal" in that its object model is designed to map directly to WinRT with no impedance mismatch - hence refcounting - but still high-level enough to avoid boilerplate and be generally safe to use (e.g. exceptions rather than HRESULTs, strings seen as objects and not handles, dynamic_cast
rather than QueryInterface
etc). No additional layers, proxy objects etc between you and WinRT, all calls are direct. In most cases, fastest of all three, though the exact difference varies significantly depending on the specific task, and can be minuscule for some (e.g. event-driven app with no or little computation), and considerable for others (e.g. parsing or heavy math). UI story is same as for .NET. In addition, you get the entire C++ standard library available to you, as well as a subset of ATL. Can write your own WinRT components, which can then be used from JS or .NET. Existing C++ libraries may or may not be readily reusable, depending on which APIs they use; those that relies strictly on Standard C/C++ will usually work with no changes, while those that call Win32 APIs may pose a problem if they rely on APIs not available in Metro app container.
C++/CX(Visual C++ 组件扩展)——低级/中级,静态类型,无 GC——仅引用计数。最接近“金属”,因为它的对象模型被设计为直接映射到 WinRT,没有阻抗不匹配 - 因此引用计数 - 但仍然足够高级以避免样板并且通常可以安全使用(例如异常而不是 HRESULT,看到的字符串作为对象而不是句柄,dynamic_cast
而不是QueryInterface
等等)。您和 WinRT 之间没有额外的层、代理对象等,所有调用都是直接的。在大多数情况下,三者中最快的,尽管确切的差异因特定任务而异,并且对于某些(例如,没有或很少计算的事件驱动应用程序)可能很小,而对于其他人(例如解析或繁重的数学运算) )。UI 故事与 .NET 相同。此外,您可以获得整个 C++ 标准库以及 ATL 的一个子集。可以编写自己的 WinRT 组件,然后可以从 JS 或 .NET 中使用。现有的 C++ 库可能容易重用,也可能不容易重用,这取决于它们使用的 API;那些严格依赖于标准 C/C++ 的应用程序通常无需更改即可运行,而那些调用 Win32 API 的应用程序如果依赖于 Metro 应用程序容器中不可用的 API,则可能会带来问题。
Regarding #3, this video - http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-789C- should answer most of your questions regarding the use of Win32 (which I presume what "low-level DLL" means) from Metro apps. Note that while the video is about C++, this also fully applies to C#, as P/Invoke and COM Interop are still there. So if you can call it from C++, you can call it from C#.
关于#3,这个视频 - http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-789C- 应该回答你关于 Win32 使用的大部分问题(我认为“低级 DLL”是什么意思) ) 来自 Metro 应用程序。请注意,虽然视频是关于 C++ 的,但这也完全适用于 C#,因为 P/Invoke 和 COM Interop 仍然存在。所以如果你能从 C++ 调用它,你就可以从 C# 调用它。
回答by Steve Rowe
1) The point of allowing language choice is to let you choose a language for the intrinsic advantages of the language and not because it is the only way to access an API. If you like dynamic languages, choose JavaScript. If you like static typing, but don't want to deal with memory, use C#. If you want the fastest execution (but the most ability to shoot yourself in the foot), choose C++.
1) 允许语言选择的目的是让你选择一种语言的内在优势,而不是因为它是访问 API 的唯一途径。如果您喜欢动态语言,请选择 JavaScript。如果您喜欢静态类型,但又不想处理内存问题,请使用 C#。如果您想要最快的执行速度(但最有能力用脚射击自己),请选择 C++。
2) That depends on what you mean by native. If you just mean that you want them to look like Metro style apps, the best way is to use the WinJS libraries that ship with the developer preview SDK.
2)这取决于你所说的原生是什么意思。如果您只是想让它们看起来像 Metro 风格的应用程序,最好的方法是使用开发者预览版 SDK 附带的 WinJS 库。
3) WinRT gives you the ability to write and call your own C++ DLLs or C# Assemblies from your JavaScript code. The restriction is that you have to expose the DLL as a WinRT object and you can't call any functions that are not otherwise allowed to be used in Metro style apps.
3) WinRT 使您能够从您的 JavaScript 代码编写和调用您自己的 C++ DLL 或 C# 程序集。限制是您必须将 DLL 公开为 WinRT 对象,并且您不能调用任何不允许在 Metro 风格应用程序中使用的函数。
回答by Ben Voigt
Same differences as they have always had. There's no such thing as C# without automatic memory management. Managed languages will have similar overhead as always.
If it runs Javascript, you should be able to use jQuery (which is pure javascript). You may need to call some MS functions for initialization, etc., but existing script functions ought to still run.
The most reliable sources I've seen have indicated that (at least mostly) WinRT sits on top of Win32. That "Windows Kernel Services" block is Win32's
kernel32.dll
. Some upper-level Win32 stuff isn't used in Metro, but what application ever used ALL of Win32?
与他们一直存在的差异相同。没有自动内存管理就没有 C# 之类的东西。托管语言将像往常一样有类似的开销。
如果它运行 Javascript,您应该能够使用 jQuery(它是纯 javascript)。您可能需要调用一些 MS 函数进行初始化等,但现有的脚本函数应该仍然运行。
我见过的最可靠的消息来源表明(至少大部分)WinRT 位于 Win32 之上。“Windows 内核服务”块是 Win32 的
kernel32.dll
. Metro 中没有使用一些上层 Win32 的东西,但是有什么应用程序使用过所有的 Win32?
回答by Ian Ringrose
Other people have explain the difference between the 3 options wells, so I will not repeat it.
其他人已经解释了3个选项井的区别,在此不再赘述。
However I think it comes done to:
但是我认为这样做是为了:
- Choose what youknow
- Choose what lets you reuse the most code
- 选择你知道的
- 选择什么让您重用最多的代码
So
所以
- If you are a .net programmer then use C# or VB.Net
- If you are porting an app from the windows phone use C# or VB.NET
- If you have a big C++ code base, then consider using unmanaged C++ with WinRT
- If you have a website and wish to provide an off-line version of it, you may get good code (and skill) reuse by using JavaScript with HTML
- If youlike JavaScript and HTML, but don't like .Net then use….
- Etc
- 如果您是 .net 程序员,请使用 C# 或 VB.Net
- 如果您要从 Windows Phone 移植应用程序,请使用 C# 或 VB.NET
- 如果您有大量 C++ 代码库,请考虑将非托管 C++ 与 WinRT 结合使用
- 如果您有一个网站并希望提供它的离线版本,您可以通过使用 JavaScript 和 HTML 获得良好的代码(和技能)重用
- 如果您喜欢 JavaScript 和 HTML,但不喜欢 .Net,那么使用...。
- 等等
回答by kingster
This video shows how to call your own C++ code from JScript:
该视频展示了如何从 JScript 调用您自己的 C++ 代码:
http://channel9.msdn.com/posts/Raman-Sharma-Building-Metro-Style-Apps-with-C-and-JavaScript
http://channel9.msdn.com/posts/Raman-Sharma-Building-Metro-Style-Apps-with-C-and-JavaScript
回答by paulsm4
Suggestion:
建议:
- Why don't you download the developer preview and look for yourself:
- 何不下载开发者预览版,自己找找看:
http://msdn.microsoft.com/en-us/windows/apps/br229516
http://msdn.microsoft.com/en-us/windows/apps/br229516
Facts:
事实:
Of course you'll still be able to use Win32 .dll's (at one level or another), just like you can with .Net.
Windows 8 is officially over a year out: there's no way to say at this point what specific "features" and "capabilities" will be in the final release.
当然,您仍然可以使用 Win32 .dll(在一个或另一个级别),就像使用 .Net 一样。
Windows 8 正式发布一年多了:目前无法确定最终版本中将包含哪些特定的“特性”和“功能”。