windows8开发】C++开发WinRT组件和JS调用

windows8开发】C++开发WinRT组件和JS调用

分类: window8 App开发   2213人阅读  评论(7)  收藏  举报
      通过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文件:
[cpp]  view plain copy
  1. #pragma once  
  2. #include <amp.h>  
  3. #include <collection.h>  
  4.   
  5. namespace TestLib  
  6. {  
  7.     public ref class WinRTComponent sealed  
  8.     {  
  9.     public:  
  10.         WinRTComponent();  
  11.   
  12.               void SyncFunc(int number);  
  13.               Windows::Foundation::IAsyncOperationWithProgress<Windows::Foundation::Collections::IVector<int>^, double>^ AsyncFunc(int number);  
  14.   
  15.        private:  
  16.               bool is_prime(int n);  
  17.     };  
  18. }  

组件中类名为 WinRTComponent,作为组件类,考虑到外部调用,声明为public,同时也声明为引用类型ref,它包含两个public方法,
SyncFunc方法为以同步调用方式计算出number值以下所有的质数,并返回结果
AsyncFunc方法为以异步调用方式计算出number以下所有的质数,并返回结果

cpp文件:
[cpp]  view plain copy
  1. #include "pch.h"  
  2. #include "WinRTComponent.h"  
  3. using namespace TestLib;  
  4. using namespace Platform;  
  5. using namespace Concurrency;  
  6. using namespace Platform::Collections;  
  7. using namespace Windows::Foundation::Collections;  
  8. using namespace Windows::Foundation;  
  9.   
  10. WinRTComponent::WinRTComponent()  
  11. {  
  12. }  
  13.   
  14. bool WinRTComponent::is_prime(int n)  
  15. {  
  16.    if (n < 2)  
  17.       return false;  
  18.    for (int i = 2; i < n; ++i)  
  19.    {  
  20.       if ((n % i) == 0)  
  21.          return false;  
  22.    }  
  23.    return true;  
  24. }  
  25.   
  26. void WinRTComponent::SyncFunc(int number)  
  27. {  
  28.        auto primes = ref new Vector<int>();  
  29.        for (int i = 0; i < number; i++) {  
  30.               if (is_prime(i)) {  
  31.                      primes->Append(i);  
  32.               }  
  33.        }  
  34. }  
  35.   
  36. IAsyncOperationWithProgress<IVector<int>^, double>^ WinRTComponent::AsyncFunc(int number)  
  37. {  
  38.        return create_async([this, number](progress_reporter<double> reporter)-> IVector<int>^ {  
  39.               auto primes = ref new Vector<int>();  
  40.               double progress = 0.0;  
  41.               double curProgress = 0.0;  
  42.               for (int i = 0; i < number; i++) {  
  43.                      if (this->is_prime(i)) {  
  44.                            primes->Append(i);  
  45.                      }  
  46.                      curProgress = 100.0 * i / number;  
  47.                      if (curProgress > progress) {  
  48.                            progress = curProgress;  
  49.                            reporter.report(curProgress);  
  50.                      }  
  51.               }  
  52.               reporter.report(100.0);  
  53.               return primes;  
  54.        });  
  55. }  

AsyncFunc方法中, create_async函数是通过异步的方式创建一个异步调用,使计算在后台进行,而程序不会阻塞在质数统计的计算中。其中有个匿名函数Lambda表达式的使用,这是C++ 0x/11中支持的新特性,不明白可朋友可以看我另外一篇博客。

另外,以上代码中ref,^等与传统C++不同的特性在本文中就不作说明了,在这个系列的其他文章中会有说明。

最后是javascript对以上组件dll的调用,代码如下:

[javascript]  view plain copy
  1. // 新建WinRTComponent对象  
  2. var testlib = new TestLib.WinRTComponent();  
  3. // 异步方法调用  
  4. testlib.asyncFunc(1000).then(  
  5.     function (v) {  
  6.         // get result from v  
  7.     },  
  8.     function (error) {  
  9.     },  
  10.     function (p) {  
  11.         // get progress  
  12.     }  
  13. );  
  14. // 同步方法调用  
  15. var vec = testlib.syncFunc(1000);  

异步方法调用后会立刻返回,在后台计算结束后会调用第一个回调函数,可以从参数v中取得计算结果。另外在计算中途,可以从最后一个回调函数中得到后台计算的进度。

总的来说,组件的封装和调用还是挺简单的,大家的意见又如何呢?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值