通过Windows(Phone) Runtime Component来分享你的代码

来源:互联网 发布:ubuntu重新安装vim 编辑:程序博客网 时间:2024/06/08 07:43
What is a Windows Runtime Component?

A Windows Runtime Component, sometimes referred to as a 3rd-party Windows Runtime Component, is a component you build to expose functionality to all supported languages. By using the Windows Runtime, which is supported on Windows Phone 8 and Windows 8, you can create components and call them in a simple and natural way from the language of your choice. The component is built using the Windows Runtime Component template in Visual Studio. Its functionality is output as a .winmd file, which contains the metadata needed to project the functionality automatically into C#, C++, VB and JavaScript.

When to use a Windows Runtime Component when building an app for Windows Phone 8 and Windows 8 

The following list describes when you should consider sharing functionality of your app in Windows Runtime Component.

  • Harnessing the power of C++(驾驭C++强大的力量的时候): If your app carries out computationally intensive operations, you might opt to write that code in C++ to achieve optimal performance. If the code that uses this functionality is written in C# or VB, you can package the functionality as a Windows Runtime Component and call the component from your managed code.

  • Crossing language boundaries(跨语言编程的时候): In general, using a Windows Runtime Component is a great way to expose your code to all supported languages. This is not necessary if your app is written in one language and you don’t intend to expose any of your functionality to other languages. Instead you can compose your app of regular class libraries, Portable Class Libraries or, depending on the size and complexity of your app, just write the whole app in the same project.

  • Using Direct3D, Win32 or COM APIs(使用系统提供的只能使用C++调用的接口的时候): Windows Phone 8 delivers native API to enable powerful gaming scenarios, native networking and access to camera API from native code. To use these API from managed code, you can wrap this functionality in a Windows Runtime Component. For more info, see Supported Win32 APIs for Windows Phone 8 and Games for Windows Phone.

  • Using 3rd party libraries(使用第三方库的时候): In the past, it was difficult to consume a 3rd-party library, written in one language, if your app written in another language. With Windows Runtime Components, it is possible to grab a library, wrap it in a Windows Runtime Component and expose the functionality to your app written in any supported language.

The preceding list is some of the scenarios where writing a Windows Runtime Component is a very useful way of sharing code. However, a Windows Runtime Component is not a standard DLL or assembly. There are very specific requirements you must follow when creating a Windows Runtime Component. These requirements include using only allowed data types, handling collections, creating only sealed classes and many more. Visual Studio will help at compile time, and indicate clearly when something goes wrong.

原创粉丝点击