Silverlight在嵌入式平台Windows ce中的应用
2010年07月23日
之前一直在web领域应用Silverlight效果还不错;随着对用户体验的不懈最求,近期公司希望将Silverlight引入嵌入式平台(Windows Embedded CE)
最近做了些探索性学习和研究!首先来个图看看我的Silverlight for windows ce 的处子Show(DEMO中哪个轮子和温度计都处于运动状态)!
第一步:创建Silverlight for Window Embedded CE开发环境(本人安装了所有VS及Windows Embedded CE 6.0 相关的组件)
1.1:所需硬件及系统环境
1.1.1:所需系统 windows XP SP2及以上版本(本人使用windows server 2003 sp2),
1.1.2:所需磁盘空间:系统盘6G+,安装盘10G+
1.1.3:保证系统干净,原有如VS2008,SL3.0,FX3.5,FX3.0等高版本的统统删除干净后重启机器;特别注意(否则可能安装不能成功)
1.1.4:其它硬件环境当然是越高越好(运行模拟器,编译OS会流畅点)
1.2:所需软件及安装顺序:
1.2.1:Visual studio 2005 TM版本(专业版本以上)
1.2.2:Visual studio 2005 TM SP1
1.2.3:Microsoft .NET Framework2.0 SP1
1.2.4:Windows Embedded CE 6.0 Evaluation Edition.exe(在线安装),如要离线安装需要完整下载Windows Embedded ce 6.0.msi及相关CAB(合计365个文件)包至本地相同目录(本人采取的方式,公司环境不能上外网)
1.2.5: Windows Embedded CE 6.0 Platform Builder Service Pack 1(WinCE 6.0 Platform Builder Service Pack 1.msi);
1.2.6: wince6.0R2(ce6R2.ISO)
1.2.7: wince6.0R3(ce6R3.ISO)
1.2.8: Microsoft .NET Framework3.5 SP1(安装 Blend 2.0所须)
1.2.9:Microsoft Expression Blend 2.0 en
1.2.10:Microsoft Expression Blend 2.0(en) SP1
第二步:定制和创建一个可以在模拟器上运行的那么多Windows Embedded CE 6.0 R3 OS(使用Platform Builder向导,并用开发机器模拟一个硬件平台)
第三步:添加Catalog items选项以支持Silverlight
3.1:设置Catalog Item View ->Core OS ->Shell and User Interface -> User Interface->Silverlight for windows Embedded(选中选项或者设置系统变量SYSGEN_XAML_RUNTIME=1)
3.2:注意事项一编译错误"Getuserinfo未定义"解决方法(网上说是CE bug为VS选项添加引用)VS->选项->项目和解决方案->VC++目录:wince600\public\common\oak\inc
3.3:注意事项二 如需在非Debug模式下启用模拟器则:解决方案资源管理器->项目->属性->配置属性->Build Options->取消 Enable KITL(no IMGNOKITL=1)
3.4:编译OS生成(这个过程比较慢似开发机器的配置,由于我选择选项比较多编译了将进40分钟才完成,故建议做好所有设置后再谨慎编译)
第四步:在Subprojects目录下创建子项目(这个比较简单按提示一步一步进行这样的方式会将项目编译进操作系统)
4.1:选择你编译的OS Project->下一步 选择WCE Application 给子项目命名->下一步 选择创建 An empty subproject完成
4.2:设置添加子项目 sources文件目标类库节点如:
TARGETLIBS= \
$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\co redll.lib \
$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\xa mlruntime.lib\
$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\uu id.lib
4.3:在子项目Parameter files文件夹下ProjSysgen.bat中加入"set SYSGEN_SAMPLEXAMLPERF=1"参数命令
4.4:如果编译还出错则尝试点击子项目右键属性 ->link-> Additional libraries中输入:$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\co redll.lib $(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\xa mlruntime.lib $(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\uu id.lib
上属过程中如果出现什么头文件找不到,库文件找不到尝试将文件拷贝至项目根目录或者将相关路径添加至VS属性VC++目录以实现引用;
4.5:详细页面代码:
4.5.1 XAML文件page.xml 4.5.2:cpp源码: // sl_demo.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "pwinuser.h" #include "xamlruntime.h" #include "xrdelegate.h" #include "xrptr.h" #include "resource.h" #include #include using std::string; UINT exitcode; IXRVisualHostPtr vhost; IXRStoryboardPtr sbRun; IXRStoryboardPtr sbOil; IXRTextBlockPtr txtOil; float oilValue=0; bool isAdd = false; class BtnEventHandler //按钮点击事件 { public: HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args) { BSTR btnName; if(FAILED(source->GetName(&btnName))) return -1; char* pstr1; pstr1=(char*)btnName; if (_tcscmp((WCHAR*)pstr1,L"btnStop")==0) { sbRun ->Stop(); } else if (_tcscmp((WCHAR*)pstr1,L"btnRun")==0) { sbRun ->Stop(); sbRun ->Begin(); } else { MessageBox(NULL,TEXT("Exit Application!"),TEXT("Silverlight for Embedded test"),MB_OK); // ExitProcess(NULL); vhost->EndDialog(exitcode); } /* _bstr_t copName(btnName,true); _bstr_t btnRun("btnRun"); _bstr_t btnStop("btnStop"); if(copName._Compare(btnRun)) { } else if (copName._Compare(btnStop)) { MessageBox(NULL,TEXT("1"),TEXT("Silverlight for Embedded test"),MB_OK); sbRun ->Stop(); } else { MessageBox(NULL,TEXT("Exit Application!"),TEXT("Silverlight for Embedded test"),MB_OK); // ExitProcess(NULL); vhost->EndDialog(exitcode); } */ return S_OK; } }; template const WCHAR* to_wstring(T const &value) { std::wstring ss; //ssGetChildren(&coll))) { int count; if(!FAILED(coll->GetCount(&count))) { for(int i=0;iGetItem(i,&frames))) { IXRDoubleKeyFrameCollection* framecoll; if(!FAILED(frames->GetKeyFrames(&framecoll))) { IXRSplineDoubleKeyFrame* kf0; IXRSplineDoubleKeyFrame* kf1; framecoll->GetItem(0,&kf0); framecoll->GetItem(1,&kf1); float tempValue; if(oilValue =125) { isAdd=false; } if(isAdd) { oilValue+=5; } else { oilValue-=10; } kf1->GetValue(&tempValue); kf0->SetValue(tempValue); kf1->SetValue(oilValue); sbOil->Begin(); } } } } } } if(txtOil != NULL) //由于本人C++刚摸索页面字符显示乱码解决中望高手指点 { /*int decimal, sign,err; WCHAR txt=(WCHAR)_fcvt(oilValue,6,&decimal,&sign); MessageBox(NULL,&txt,TEXT("Silverlight for Embedded test"),MB_OK); txtOil ->SetText(&txt);*/ /*int decimal, sign,err; const char * s = _fcvt(oilValue,6,&decimal,&sign); int nlen = strlen(s); int nwLen = MultiByteToWideChar(CP_ACP,0,s,nlen,NULL,0); WCHAR lpszFile[256]; MultiByteToWideChar(CP_ACP,0,s,nlen,lpszFile,nwLen ); const WCHAR desc = (WCHAR)lpszFile;*/ txtOil ->SetText(to_wstring(&txtOil)); } return 0; } }; //页面主函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. if(!XamlRuntimeInitialize()) return -1; HRESULT retcode; IXRApplicationPtr app; if (FAILED(retcode=GetXRApplicationInstance(&app))) return -1; if (FAILED(retcode=app->AddResourceModule(hInstance))) return -1; XRWindowCreateParams wp; ZeroMemory(&wp, sizeof(XRWindowCreateParams)); wp.Style = WS_OVERLAPPED; wp.pTitle = L"Wince Test"; wp.Left = 0; wp.Top = 0; XRXamlSource xamlsrc; //动态从文件系统中加载XAML xamlsrc.SetFile(L"\\Storage Card\\Page.xaml"); /* xamlsrc.SetFile(L"\\Page.xaml"); xamlsrc.SetResource(hInstance,TEXT("XAML"),MAKEINT RESOURCE(IDR_XAML1)); */ if (FAILED(retcode=app -> CreateHostFromXaml(&xamlsrc, &wp, &vhost))) return -1; IXRFrameworkElementPtr root; if (FAILED(retcode=vhost->GetRootElement(&root))) return -1; if(FAILED(retcode=root->FindName(TEXT("sbRun"),&sbR un))) return -1; sbRun->Begin(); if(!FAILED(retcode=root->FindName(TEXT("sbOil"),&sb Oil))) //注册storyboard Complete事件 { IXRDelegate* completedelegate; CompleteEventHandler comHandler; if(FAILED(retcode = CreateDelegate(&comHandler,&CompleteEventHandler:: OnStoryBoardComplete,&completedelegate))) return -1; if(FAILED(retcode = sbOil->AddCompletedEventHandler(completedelegate))) return -1; sbOil ->Begin(); } //注册按钮点击事件 IXRButtonBasePtr btn; IXRButtonBasePtr btnRun; IXRButtonBasePtr btnStop; //IXRCanvasPtr btn; if (FAILED(retcode=root->FindName(TEXT("btnDialog"), &btn))) return -1; if (FAILED(retcode=root->FindName(TEXT("btnRun"), &btnRun))) return -1; if (FAILED(retcode=root->FindName(TEXT("btnStop"), &btnStop))) return -1; retcode=root->FindName(TEXT("txtOil"),&txtOil); IXRDelegate* clickdelegate; BtnEventHandler handler; if (FAILED(retcode=CreateDelegate(&handler,&BtnEventH andler::OnClick,&clickdelegate))) return -1; if (FAILED(retcode=btn->AddClickEventHandler(clickdele gate))) return -1; if (FAILED(retcode=btnRun->AddClickEventHandler(clickd elegate))) return -1; if (FAILED(retcode=btnStop->AddClickEventHandler(click delegate))) return -1; clickdelegate->Release(); MessageBox(NULL,TEXT("load Successful!"),TEXT("Silverlight for Embedded test"),MB_OK); if (FAILED(retcode=vhost->StartDialog(&exitcode))) return -1; return 0; } 如需演示DEMO请留脚印!
下一节将介绍如何生成SDK,使用SDK进行开发,并在SL中实现资源()加载及应用全局样式!
2010年07月23日
之前一直在web领域应用Silverlight效果还不错;随着对用户体验的不懈最求,近期公司希望将Silverlight引入嵌入式平台(Windows Embedded CE)
最近做了些探索性学习和研究!首先来个图看看我的Silverlight for windows ce 的处子Show(DEMO中哪个轮子和温度计都处于运动状态)!
第一步:创建Silverlight for Window Embedded CE开发环境(本人安装了所有VS及Windows Embedded CE 6.0 相关的组件)
1.1:所需硬件及系统环境
1.1.1:所需系统 windows XP SP2及以上版本(本人使用windows server 2003 sp2),
1.1.2:所需磁盘空间:系统盘6G+,安装盘10G+
1.1.3:保证系统干净,原有如VS2008,SL3.0,FX3.5,FX3.0等高版本的统统删除干净后重启机器;特别注意(否则可能安装不能成功)
1.1.4:其它硬件环境当然是越高越好(运行模拟器,编译OS会流畅点)
1.2:所需软件及安装顺序:
1.2.1:Visual studio 2005 TM版本(专业版本以上)
1.2.2:Visual studio 2005 TM SP1
1.2.3:Microsoft .NET Framework2.0 SP1
1.2.4:Windows Embedded CE 6.0 Evaluation Edition.exe(在线安装),如要离线安装需要完整下载Windows Embedded ce 6.0.msi及相关CAB(合计365个文件)包至本地相同目录(本人采取的方式,公司环境不能上外网)
1.2.5: Windows Embedded CE 6.0 Platform Builder Service Pack 1(WinCE 6.0 Platform Builder Service Pack 1.msi);
1.2.6: wince6.0R2(ce6R2.ISO)
1.2.7: wince6.0R3(ce6R3.ISO)
1.2.8: Microsoft .NET Framework3.5 SP1(安装 Blend 2.0所须)
1.2.9:Microsoft Expression Blend 2.0 en
1.2.10:Microsoft Expression Blend 2.0(en) SP1
第二步:定制和创建一个可以在模拟器上运行的那么多Windows Embedded CE 6.0 R3 OS(使用Platform Builder向导,并用开发机器模拟一个硬件平台)
第三步:添加Catalog items选项以支持Silverlight
3.1:设置Catalog Item View ->Core OS ->Shell and User Interface -> User Interface->Silverlight for windows Embedded(选中选项或者设置系统变量SYSGEN_XAML_RUNTIME=1)
3.2:注意事项一编译错误"Getuserinfo未定义"解决方法(网上说是CE bug为VS选项添加引用)VS->选项->项目和解决方案->VC++目录:wince600\public\common\oak\inc
3.3:注意事项二 如需在非Debug模式下启用模拟器则:解决方案资源管理器->项目->属性->配置属性->Build Options->取消 Enable KITL(no IMGNOKITL=1)
3.4:编译OS生成(这个过程比较慢似开发机器的配置,由于我选择选项比较多编译了将进40分钟才完成,故建议做好所有设置后再谨慎编译)
第四步:在Subprojects目录下创建子项目(这个比较简单按提示一步一步进行这样的方式会将项目编译进操作系统)
4.1:选择你编译的OS Project->下一步 选择WCE Application 给子项目命名->下一步 选择创建 An empty subproject完成
4.2:设置添加子项目 sources文件目标类库节点如:
TARGETLIBS= \
$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\co redll.lib \
$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\xa mlruntime.lib\
$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\uu id.lib
4.3:在子项目Parameter files文件夹下ProjSysgen.bat中加入"set SYSGEN_SAMPLEXAMLPERF=1"参数命令
4.4:如果编译还出错则尝试点击子项目右键属性 ->link-> Additional libraries中输入:$(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\co redll.lib $(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\xa mlruntime.lib $(_PROJECTROOT)\cesysgen\sdk\lib\$(_CPUINDPATH)\uu id.lib
上属过程中如果出现什么头文件找不到,库文件找不到尝试将文件拷贝至项目根目录或者将相关路径添加至VS属性VC++目录以实现引用;
4.5:详细页面代码:
4.5.1 XAML文件page.xml 4.5.2:cpp源码: // sl_demo.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "pwinuser.h" #include "xamlruntime.h" #include "xrdelegate.h" #include "xrptr.h" #include "resource.h" #include #include using std::string; UINT exitcode; IXRVisualHostPtr vhost; IXRStoryboardPtr sbRun; IXRStoryboardPtr sbOil; IXRTextBlockPtr txtOil; float oilValue=0; bool isAdd = false; class BtnEventHandler //按钮点击事件 { public: HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args) { BSTR btnName; if(FAILED(source->GetName(&btnName))) return -1; char* pstr1; pstr1=(char*)btnName; if (_tcscmp((WCHAR*)pstr1,L"btnStop")==0) { sbRun ->Stop(); } else if (_tcscmp((WCHAR*)pstr1,L"btnRun")==0) { sbRun ->Stop(); sbRun ->Begin(); } else { MessageBox(NULL,TEXT("Exit Application!"),TEXT("Silverlight for Embedded test"),MB_OK); // ExitProcess(NULL); vhost->EndDialog(exitcode); } /* _bstr_t copName(btnName,true); _bstr_t btnRun("btnRun"); _bstr_t btnStop("btnStop"); if(copName._Compare(btnRun)) { } else if (copName._Compare(btnStop)) { MessageBox(NULL,TEXT("1"),TEXT("Silverlight for Embedded test"),MB_OK); sbRun ->Stop(); } else { MessageBox(NULL,TEXT("Exit Application!"),TEXT("Silverlight for Embedded test"),MB_OK); // ExitProcess(NULL); vhost->EndDialog(exitcode); } */ return S_OK; } }; template const WCHAR* to_wstring(T const &value) { std::wstring ss; //ssGetChildren(&coll))) { int count; if(!FAILED(coll->GetCount(&count))) { for(int i=0;iGetItem(i,&frames))) { IXRDoubleKeyFrameCollection* framecoll; if(!FAILED(frames->GetKeyFrames(&framecoll))) { IXRSplineDoubleKeyFrame* kf0; IXRSplineDoubleKeyFrame* kf1; framecoll->GetItem(0,&kf0); framecoll->GetItem(1,&kf1); float tempValue; if(oilValue =125) { isAdd=false; } if(isAdd) { oilValue+=5; } else { oilValue-=10; } kf1->GetValue(&tempValue); kf0->SetValue(tempValue); kf1->SetValue(oilValue); sbOil->Begin(); } } } } } } if(txtOil != NULL) //由于本人C++刚摸索页面字符显示乱码解决中望高手指点 { /*int decimal, sign,err; WCHAR txt=(WCHAR)_fcvt(oilValue,6,&decimal,&sign); MessageBox(NULL,&txt,TEXT("Silverlight for Embedded test"),MB_OK); txtOil ->SetText(&txt);*/ /*int decimal, sign,err; const char * s = _fcvt(oilValue,6,&decimal,&sign); int nlen = strlen(s); int nwLen = MultiByteToWideChar(CP_ACP,0,s,nlen,NULL,0); WCHAR lpszFile[256]; MultiByteToWideChar(CP_ACP,0,s,nlen,lpszFile,nwLen ); const WCHAR desc = (WCHAR)lpszFile;*/ txtOil ->SetText(to_wstring(&txtOil)); } return 0; } }; //页面主函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. if(!XamlRuntimeInitialize()) return -1; HRESULT retcode; IXRApplicationPtr app; if (FAILED(retcode=GetXRApplicationInstance(&app))) return -1; if (FAILED(retcode=app->AddResourceModule(hInstance))) return -1; XRWindowCreateParams wp; ZeroMemory(&wp, sizeof(XRWindowCreateParams)); wp.Style = WS_OVERLAPPED; wp.pTitle = L"Wince Test"; wp.Left = 0; wp.Top = 0; XRXamlSource xamlsrc; //动态从文件系统中加载XAML xamlsrc.SetFile(L"\\Storage Card\\Page.xaml"); /* xamlsrc.SetFile(L"\\Page.xaml"); xamlsrc.SetResource(hInstance,TEXT("XAML"),MAKEINT RESOURCE(IDR_XAML1)); */ if (FAILED(retcode=app -> CreateHostFromXaml(&xamlsrc, &wp, &vhost))) return -1; IXRFrameworkElementPtr root; if (FAILED(retcode=vhost->GetRootElement(&root))) return -1; if(FAILED(retcode=root->FindName(TEXT("sbRun"),&sbR un))) return -1; sbRun->Begin(); if(!FAILED(retcode=root->FindName(TEXT("sbOil"),&sb Oil))) //注册storyboard Complete事件 { IXRDelegate* completedelegate; CompleteEventHandler comHandler; if(FAILED(retcode = CreateDelegate(&comHandler,&CompleteEventHandler:: OnStoryBoardComplete,&completedelegate))) return -1; if(FAILED(retcode = sbOil->AddCompletedEventHandler(completedelegate))) return -1; sbOil ->Begin(); } //注册按钮点击事件 IXRButtonBasePtr btn; IXRButtonBasePtr btnRun; IXRButtonBasePtr btnStop; //IXRCanvasPtr btn; if (FAILED(retcode=root->FindName(TEXT("btnDialog"), &btn))) return -1; if (FAILED(retcode=root->FindName(TEXT("btnRun"), &btnRun))) return -1; if (FAILED(retcode=root->FindName(TEXT("btnStop"), &btnStop))) return -1; retcode=root->FindName(TEXT("txtOil"),&txtOil); IXRDelegate* clickdelegate; BtnEventHandler handler; if (FAILED(retcode=CreateDelegate(&handler,&BtnEventH andler::OnClick,&clickdelegate))) return -1; if (FAILED(retcode=btn->AddClickEventHandler(clickdele gate))) return -1; if (FAILED(retcode=btnRun->AddClickEventHandler(clickd elegate))) return -1; if (FAILED(retcode=btnStop->AddClickEventHandler(click delegate))) return -1; clickdelegate->Release(); MessageBox(NULL,TEXT("load Successful!"),TEXT("Silverlight for Embedded test"),MB_OK); if (FAILED(retcode=vhost->StartDialog(&exitcode))) return -1; return 0; } 如需演示DEMO请留脚印!
下一节将介绍如何生成SDK,使用SDK进行开发,并在SL中实现资源()加载及应用全局样式!