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

第一种方法:http://blog.csdn.net/91program/article/details/36675607

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

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


再包含 Silverlight 库文件:
#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 的使用。


代码:
// SilverlightHelloWorld2.cpp : 定义应用程序的入口点。
//


#include "stdafx.h"
#include "SilverlightHelloWorld2.h"


// #include "pwinuser.h"
#include "xamlruntime.h"
#include "xrdelegate.h"
#include "xrptr.h"


#include "resource.h"


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


class BtnEventHandler
{
public:


	HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args)
	{
		MessageBox(NULL,TEXT("Click!"),TEXT("Silverlight for Windows Embedded test"),MB_OK);
		return S_OK;
	}
};


int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	// 初始化 XAML 运行时库
	if (!XamlRuntimeInitialize())
		return -1;


	HRESULT retcode;


	IXRApplicationPtr app;


	// 每个 WinCE Silverlight应用有一个 "Application" 对象, 允许访问全局的资源.
	// 使用  GetXRApplicationInstance API 来获取此应用对象。
	if (FAILED(retcode = GetXRApplicationInstance(&app)))
		return -1;
	// 声明 resources (XAML, images etc.) 在哪里.
	if (FAILED(retcode = app->AddResourceModule(hInstance)))
		return -1;


	// 初始化应用对象, 创建 Silverlight 管理的主窗口.
	XRWindowCreateParams wp;


	ZeroMemory(&wp, sizeof(XRWindowCreateParams));
	wp.Style       = WS_OVERLAPPED;
	wp.pTitle      = L"Silverlight4WinCESample";
	wp.Left        = 0;
	wp.Top         = 0;


	// 从资源中加载 XAML(使用 XRXamlSource 对象).
	// 通过 IXRVisualHostPtr 对象 vhost 在运行时访问 XAML 的内容.
	XRXamlSource xamlsrc;


	xamlsrc.SetResource(hInstance,TEXT("XAML"),MAKEINTRESOURCE(IDR_XAML1));


	IXRVisualHostPtr vhost;


	if (FAILED(retcode = app->CreateHostFromXaml(&xamlsrc, &wp, &vhost)))
		return -1;


	IXRFrameworkElementPtr root;


	if (FAILED(retcode = vhost->GetRootElement(&root)))
		return -1;


	IXRButtonBasePtr btn;


	if (FAILED(retcode = root->FindName(TEXT("HelloWorldBtn"), &btn)))
		return -1;


	// 当用户点击按键时,使用 delegate 来接收通知.
	BtnEventHandler handler;


	IXRDelegate<XRMouseButtonEventArgs> *pClickDelegate;


	if (FAILED(retcode = CreateDelegate(&handler,&BtnEventHandler::OnClick,&pClickDelegate)))
		return -1;


	if (FAILED(retcode = btn->AddClickEventHandler(pClickDelegate)))
		return -1;


	// Silverlight 界面显示
	UINT exitcode;


	if (FAILED(retcode = vhost->StartDialog(&exitcode)))
		return -1;


	// 由于 pClickDelegate 对象不是智能指针, 所以必须显式释放它.
	pClickDelegate->Release();


	return 0;
}



//MSDN 动态按键示例:  http://msdn.microsoft.com/en-us/library/ee504258.aspx
#include <windows.h>
#include <XamlRuntime.h>
#include <XRPtr.h>




void AddElementToTree(IXRApplication* pApplication, IXRVisualHost* pVisualHost)
{ 
  IXRButtonPtr greenButton;
  float btnHeight = 6;
  float btnWidth = 12;


  IXRCanvasPtr pCanvas;
  IXRUIElementCollectionPtr pnlChildren;


  // Create a new UI element
  pApplication->CreateObject(&greenButton);


  greenButton->SetHeight(btnHeight);
  greenButton->SetWidth(btnWidth);
  greenButton->SetName(L"MyGreenButton");


  // Obtain a pointer to the root of the visual tree
  IXRFrameworkElementPtr pRootElement;
  pVisualHost->GetRootElement(&pRootElement);


  // Traverse the visual tree and find a named canvas object
  pRootElement->FindName(L"MyCanvas", &pCanvas); 


  // Get the collection of elements from the located canvas object
  pCanvas->GetChildren(&pnlChildren);


  // Add the new UI element to the collection
  pnlChildren->Add(greenButton, NULL);
}


你可能感兴趣的:(另一种基于 WinCE 的 Silverlight 应用建立过程)