另一种基于 WinCE 的 Silverlight 应用建立过程

第一种方法:http://www.cnblogs.com/91program/p/5201231.html

这种方法相对来说比较简单。

基于 WinCE 的 Silverlight 只支持 C++ 语言编程,但 Expression Blend 只能生成 C# 和 VB.Net,所以不能使用 Expression Blend 生成的代码。
同样,创建 Win32 应用。
首先,包含以下 Silverlight 头文件:

1 #include "xamlruntime.h"  
2 #include "xrdelegate.h"  
3 #include "xrptr.h"  

再包含 Silverlight 库文件:

1 #pragma comment(lib,"xamlruntime.lib")  

将 XAML 文件当做资源增加到工程中,具体的操作如下:
1) 在“资源视图(resource view)”页,右键单击资源,选择“增加资源(add resource...)”
2) 点击“导入(import)”按键,选择需要导入的 XAML 文件
3) 输入资源的类型: XAML
4) 你可以保留默认的资源 ID:IDR_XAML1,但在实际的项目中建议给资源一个形象的名字。
5) 包含 resource.h 到 cpp 中,以方便对资源 ID 的使用。

代码:

  1 // SilverlightHelloWorld2.cpp : 定义应用程序的入口点。  
  2 //  
  3   
  4   
  5 #include "stdafx.h"  
  6 #include "SilverlightHelloWorld2.h"  
  7   
  8   
  9 // #include "pwinuser.h"  
 10 #include "xamlruntime.h"  
 11 #include "xrdelegate.h"  
 12 #include "xrptr.h"  
 13   
 14   
 15 #include "resource.h"  
 16   
 17   
 18 #pragma comment(lib,"xamlruntime.lib")  
 19   
 20   
 21 class BtnEventHandler  
 22 {  
 23 public:  
 24   
 25   
 26     HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)  
 27     {  
 28         MessageBox(NULL,TEXT("Click!"),TEXT("Silverlight for Windows Embedded test"),MB_OK);  
 29         return S_OK;  
 30     }  
 31 };  
 32   
 33   
 34 int WINAPI WinMain(HINSTANCE hInstance,  
 35                    HINSTANCE hPrevInstance,  
 36                    LPTSTR    lpCmdLine,  
 37                    int       nCmdShow)  
 38 {  
 39     // 初始化 XAML 运行时库  
 40     if (!XamlRuntimeInitialize())  
 41         return -1;  
 42   
 43   
 44     HRESULT retcode;  
 45   
 46   
 47     IXRApplicationPtr app;  
 48   
 49   
 50     // 每个 WinCE Silverlight应用有一个 "Application" 对象, 允许访问全局的资源.  
 51     // 使用  GetXRApplicationInstance API 来获取此应用对象。  
 52     if (FAILED(retcode = GetXRApplicationInstance(&app)))  
 53         return -1;  
 54     // 声明 resources (XAML, images etc.) 在哪里.  
 55     if (FAILED(retcode = app->AddResourceModule(hInstance)))  
 56         return -1;  
 57   
 58   
 59     // 初始化应用对象, 创建 Silverlight 管理的主窗口.  
 60     XRWindowCreateParams wp;  
 61   
 62   
 63     ZeroMemory(&wp, sizeof(XRWindowCreateParams));  
 64     wp.Style       = WS_OVERLAPPED;  
 65     wp.pTitle      = L"Silverlight4WinCESample";  
 66     wp.Left        = 0;  
 67     wp.Top         = 0;  
 68   
 69   
 70     // 从资源中加载 XAML(使用 XRXamlSource 对象).  
 71     // 通过 IXRVisualHostPtr 对象 vhost 在运行时访问 XAML 的内容.  
 72     XRXamlSource xamlsrc;  
 73   
 74   
 75     xamlsrc.SetResource(hInstance,TEXT("XAML"),MAKEINTRESOURCE(IDR_XAML1));  
 76   
 77   
 78     IXRVisualHostPtr vhost;  
 79   
 80   
 81     if (FAILED(retcode = app->CreateHostFromXaml(&xamlsrc, &wp, &vhost)))  
 82         return -1;  
 83   
 84   
 85     IXRFrameworkElementPtr root;  
 86   
 87   
 88     if (FAILED(retcode = vhost->GetRootElement(&root)))  
 89         return -1;  
 90   
 91   
 92     IXRButtonBasePtr btn;  
 93   
 94   
 95     if (FAILED(retcode = root->FindName(TEXT("HelloWorldBtn"), &btn)))  
 96         return -1;  
 97   
 98   
 99     // 当用户点击按键时,使用 delegate 来接收通知.  
100     BtnEventHandler handler;  
101   
102   
103     IXRDelegate<XRMouseButtonEventArgs> *pClickDelegate;  
104   
105   
106     if (FAILED(retcode = CreateDelegate(&handler,&BtnEventHandler::OnClick,&pClickDelegate)))  
107         return -1;  
108   
109   
110     if (FAILED(retcode = btn->AddClickEventHandler(pClickDelegate)))  
111         return -1;  
112   
113   
114     // Silverlight 界面显示  
115     UINT exitcode;  
116   
117   
118     if (FAILED(retcode = vhost->StartDialog(&exitcode)))  
119         return -1;  
120   
121   
122     // 由于 pClickDelegate 对象不是智能指针, 所以必须显式释放它.  
123     pClickDelegate->Release();  
124   
125   
126     return 0;  
127 }  

//MSDN 动态按键示例: http://msdn.microsoft.com/en-us/library/ee504258.aspx

 1 #include <windows.h>  
 2 #include <XamlRuntime.h>  
 3 #include <XRPtr.h>  
 4   
 5   
 6   
 7   
 8 void AddElementToTree(IXRApplication* pApplication, IXRVisualHost* pVisualHost)  
 9 {   
10   IXRButtonPtr greenButton;  
11   float btnHeight = 6;  
12   float btnWidth = 12;  
13   
14   
15   IXRCanvasPtr pCanvas;  
16   IXRUIElementCollectionPtr pnlChildren;  
17   
18   
19   // Create a new UI element  
20   pApplication->CreateObject(&greenButton);  
21   
22   
23   greenButton->SetHeight(btnHeight);  
24   greenButton->SetWidth(btnWidth);  
25   greenButton->SetName(L"MyGreenButton");  
26   
27   
28   // Obtain a pointer to the root of the visual tree  
29   IXRFrameworkElementPtr pRootElement;  
30   pVisualHost->GetRootElement(&pRootElement);  
31   
32   
33   // Traverse the visual tree and find a named canvas object  
34   pRootElement->FindName(L"MyCanvas", &pCanvas);   
35   
36   
37   // Get the collection of elements from the located canvas object  
38   pCanvas->GetChildren(&pnlChildren);  
39   
40   
41   // Add the new UI element to the collection  
42   pnlChildren->Add(greenButton, NULL);  
43 }  

 

posted @ 2016-02-19 16:08  91program  阅读(351)  评论(0编辑  收藏  举报