【windows8开发】C++开发WinRT组件和JS调用
生活随笔
收集整理的這篇文章主要介紹了
【windows8开发】C++开发WinRT组件和JS调用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過Windows Runtime(以下簡稱WinRT),可以用C++或C#或VB很方便的開發組件(dll),并且這些組件在用Javascript開發的Metro app中可以幾乎無縫的被(javascript)調用。由于win8開發平臺下,Javascript并不能訪問原生的C++代碼(雖然可以訪問WinRT API),而實際開發中,經常會有一些既存的組件,或者需要用一些第三方庫,這時就可以考慮把這些組件或希望利用的庫包裝成WinRT組件供UI層(JS)調用, 讓我們來看一個具體的例子吧。以下代碼在Beta版VS2011中可以編譯運行。?
創建WinRT Dll工程,工程名為TestLib,代碼如下 .h文件: #pragma once #include <amp.h> #include <collection.h>namespace TestLib {public ref class WinRTComponent sealed{public:WinRTComponent();void SyncFunc(int number);Windows::Foundation::IAsyncOperationWithProgress<Windows::Foundation::Collections::IVector<int>^, double>^ AsyncFunc(int number);private:bool is_prime(int n);}; }
組件中類名為WinRTComponent,作為組件類,考慮到外部調用,聲明為public,同時也聲明為引用類型ref,它包含兩個public方法, SyncFunc方法為以同步調用方式計算出number值以下所有的質數,并返回結果 AsyncFunc方法為以異步調用方式計算出number以下所有的質數,并返回結果
cpp文件: #include "pch.h" #include "WinRTComponent.h" using namespace TestLib; using namespace Platform; using namespace Concurrency; using namespace Platform::Collections; using namespace Windows::Foundation::Collections; using namespace Windows::Foundation;WinRTComponent::WinRTComponent() { }bool WinRTComponent::is_prime(int n) {if (n < 2)return false;for (int i = 2; i < n; ++i){if ((n % i) == 0)return false;}return true; }void WinRTComponent::SyncFunc(int number) {auto primes = ref new Vector<int>();for (int i = 0; i < number; i++) {if (is_prime(i)) {primes->Append(i);}} }IAsyncOperationWithProgress<IVector<int>^, double>^ WinRTComponent::AsyncFunc(int number) {return create_async([this, number](progress_reporter<double> reporter)-> IVector<int>^ {auto primes = ref new Vector<int>();double progress = 0.0;double curProgress = 0.0;for (int i = 0; i < number; i++) {if (this->is_prime(i)) {primes->Append(i);}curProgress = 100.0 * i / number;if (curProgress > progress) {progress = curProgress;reporter.report(curProgress);}}reporter.report(100.0);return primes;}); }
AsyncFunc方法中,create_async函數是通過異步的方式創建一個異步調用,使計算在后臺進行,而程序不會阻塞在質數統計的計算中。其中有個匿名函數Lambda表達式的使用,這是C++ 0x/11中支持的新特性,不明白可朋友可以看我另外一篇博客。 http://blog.csdn.net/my_business/article/details/7477615
另外,以上代碼中ref,^等與傳統C++不同的特性在本文中就不作說明了,在這個系列的其他文章中會有說明。
最后是javascript對以上組件dll的調用,代碼如下:
// 新建WinRTComponent對象 var testlib = new TestLib.WinRTComponent(); // 異步方法調用 testlib.asyncFunc(1000).then(function (v) {// get result from v},function (error) {},function (p) {// get progress} ); // 同步方法調用 var vec = testlib.syncFunc(1000);
異步方法調用后會立刻返回,在后臺計算結束后會調用第一個回調函數,可以從參數v中取得計算結果。另外在計算中途,可以從最后一個回調函數中得到后臺計算的進度。
總的來說,組件的封裝和調用還是挺簡單的,大家的意見又如何呢?
創建WinRT Dll工程,工程名為TestLib,代碼如下 .h文件: #pragma once #include <amp.h> #include <collection.h>namespace TestLib {public ref class WinRTComponent sealed{public:WinRTComponent();void SyncFunc(int number);Windows::Foundation::IAsyncOperationWithProgress<Windows::Foundation::Collections::IVector<int>^, double>^ AsyncFunc(int number);private:bool is_prime(int n);}; }
組件中類名為WinRTComponent,作為組件類,考慮到外部調用,聲明為public,同時也聲明為引用類型ref,它包含兩個public方法, SyncFunc方法為以同步調用方式計算出number值以下所有的質數,并返回結果 AsyncFunc方法為以異步調用方式計算出number以下所有的質數,并返回結果
cpp文件: #include "pch.h" #include "WinRTComponent.h" using namespace TestLib; using namespace Platform; using namespace Concurrency; using namespace Platform::Collections; using namespace Windows::Foundation::Collections; using namespace Windows::Foundation;WinRTComponent::WinRTComponent() { }bool WinRTComponent::is_prime(int n) {if (n < 2)return false;for (int i = 2; i < n; ++i){if ((n % i) == 0)return false;}return true; }void WinRTComponent::SyncFunc(int number) {auto primes = ref new Vector<int>();for (int i = 0; i < number; i++) {if (is_prime(i)) {primes->Append(i);}} }IAsyncOperationWithProgress<IVector<int>^, double>^ WinRTComponent::AsyncFunc(int number) {return create_async([this, number](progress_reporter<double> reporter)-> IVector<int>^ {auto primes = ref new Vector<int>();double progress = 0.0;double curProgress = 0.0;for (int i = 0; i < number; i++) {if (this->is_prime(i)) {primes->Append(i);}curProgress = 100.0 * i / number;if (curProgress > progress) {progress = curProgress;reporter.report(curProgress);}}reporter.report(100.0);return primes;}); }
AsyncFunc方法中,create_async函數是通過異步的方式創建一個異步調用,使計算在后臺進行,而程序不會阻塞在質數統計的計算中。其中有個匿名函數Lambda表達式的使用,這是C++ 0x/11中支持的新特性,不明白可朋友可以看我另外一篇博客。 http://blog.csdn.net/my_business/article/details/7477615
另外,以上代碼中ref,^等與傳統C++不同的特性在本文中就不作說明了,在這個系列的其他文章中會有說明。
最后是javascript對以上組件dll的調用,代碼如下:
// 新建WinRTComponent對象 var testlib = new TestLib.WinRTComponent(); // 異步方法調用 testlib.asyncFunc(1000).then(function (v) {// get result from v},function (error) {},function (p) {// get progress} ); // 同步方法調用 var vec = testlib.syncFunc(1000);
異步方法調用后會立刻返回,在后臺計算結束后會調用第一個回調函數,可以從參數v中取得計算結果。另外在計算中途,可以從最后一個回調函數中得到后臺計算的進度。
總的來說,組件的封裝和調用還是挺簡單的,大家的意見又如何呢?
轉載于:https://www.cnblogs.com/secbook/archive/2012/04/21/2655114.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【windows8开发】C++开发WinRT组件和JS调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用JAVA开发网站,需要学哪些呢?
- 下一篇: MySQL之权限索引学习整理