新建 WinCE7.0 下的 Silverlight 工程
這也是我寫此文章時(shí)一步一步建立的 Silverlight 工程。
1 使用 Microsoft Expression Blend 3 創(chuàng)建一個(gè) Silverlight for Windows Embedded Application 工程,放一個(gè)按鍵控件在窗體上,命名按鍵然后保存。由于 Microsoft Expression Blend 3 現(xiàn)在只支持生成 C# 的代碼,對我們沒有什么用,所以我們只用其中的 XAML 文件。
2 VS2008 新建:智能設(shè)備->Win32 智能設(shè)備項(xiàng)目,選拔一個(gè)支持 Silverlight 的SDK。
3 文件 SilverlightHelloWorld.cpp 中,只保留如下代碼,其它刪除:
//?SilverlightHelloWorld.cpp?:?定義應(yīng)用程序的入口點(diǎn)。 // #include?"stdafx.h" #include?"SilverlightHelloWorld.h" int?WINAPI?WinMain(HINSTANCE?hInstance, ???????????????????HINSTANCE?hPrevInstance, ???????????????????LPTSTR????lpCmdLine, ???????????????????int???????nCmdShow) { ??return?0; }
4 新增加 4 個(gè)空文件,內(nèi)容后面補(bǔ),分別是:?
????App.cpp ????App.h ????MainPage.cpp ????MainPage.h
5 增加文件 XRPack.rules,文件的內(nèi)容如下:??
6 將 XRPack.rules 增加到工程文件 SilverlightHelloWorld.vcproj 中,修改了兩個(gè)地方,修改后的內(nèi)容如下:
......
7 新增文件 SilverlightHelloWorld.xrpack,其內(nèi)容如下:
#?This?file?is?used?to?generate?the?rc2?file?and?baml?resources #?Uncomment?/C?to?force?xrpack?to?perform?a?clean?build?every?time #?/C #?Verbosity?can?be?a?value?between?1?and?10 /Verbosity=3 /NoResourceCompile "/TargetRC=WinEmbeddedHelloWorldGenerated.rc2" "/TargetHeader=WinEmbeddedHelloWorldGenerated.h" "/Project=..WinEmbeddedHelloWorldWinEmbeddedHelloWorld.csproj"
8 修改 stdafx.h ,增加以下內(nèi)容:
//?Xaml?Runtime?Header?Files #include#include#include#include//?IUnknown extern?"C"?const?GUID?__declspec(selectany)IID_IUnknown?=?__uuidof(IUnknown); //?Resource?type?for?XAML?files #define?RT_XAML?L"XAML" //?Application?headers #include?"App.h" #include?"resource.h"
9 將 SilverlightHelloWorld.xrpack 文件增加到工程中?
這樣才能自動生成 WinEmbeddedHelloWorldGenerated.rc2 和 WinEmbeddedHelloWorldGenerated.h 文件
10 WinMain() 函數(shù)的實(shí)現(xiàn)代碼如下
int?WINAPI?WinMain(HINSTANCE?hInstance,HINSTANCE?hPrevInstance,LPTSTR?lpCmdLine,int?nCmdShow) { ??App?AppInstance; ??HRESULT?hr?=?AppInstance.Initialize(hInstance); ??if(SUCCEEDED(hr)) ??{ ????hr?=?AppInstance.Run(); ??} ??return?AppInstance.GetWinMainResultCode(); }
11 編譯,是可以成功的,但還需要根據(jù) XAML 文件的內(nèi)容來修改 MailPage.cpp 和 MailPage.h。
MailPage.h
#pragma?once //class?__declspec(uuid("{1756acb7-63be-4a4b-97cf-edc048541e75}"))?MainPage ????:?public?XRCustomUserControlImpl{ ????QI_IDENTITY_MAPPING(MainPage,?XRCustomUserControlImpl) public: ????static?HRESULT?GetXamlSource(__in?XRXamlSource*?pXamlSource) ????{ ????????HRESULT?hr?=?E_INVALIDARG; ????????if?(pXamlSource) ????????{ ????????????pXamlSource->SetResource?(App::GetHInstance(),?IDR_SILVERLIGHTCLOCK_MAINPAGE); ????????????hr?=?S_OK; ????????} ???????? ????????return?hr; ????} ????static?HRESULT?Register() ????{ ????????return?XRCustomUserControlImpl::Register?(__uuidof(MainPage),?L"MainPage",?L"clr-namespace:SilverlightClock"); ????} #pragma?region?GeneratedCode ????//?============================================================================ ????//??WARNING:?DO?NOT?EDIT?THIS?ALWAYS-GENERATED?CODE ????//?============================================================================ ????HRESULT?OnLoaded(__in?IXRDependencyObject*?pRoot); ????HRESULT?InitializeComponent(); ??IXRGridPtr?m_pLayoutRoot;?????????????????//?Grid?x:Name="LayoutRoot"?... ??IXRButtonBasePtr?m_MyBtn; ??IXRDelegate*m_clickDelegate; ????//?============================================================================ ????//??WARNING:?DO?NOT?EDIT?THIS?ALWAYS-GENERATED?CODE ????//?============================================================================ #pragma?endregion?GeneratedCode };
MailPage.cpp
#include?"stdafx.h" #include?"SilverlightHelloGenerated.h" #include?"MainPage.h" #include?"App.h" #include?"resource.h" #define?TEST_BTN_CLICK_PROCESS??1 #if?TEST_BTN_CLICK_PROCESS class?BtnEventHandler { public: ??HRESULT?OnClick(IXRDependencyObject*?source,XRMouseButtonEventArgs*?args) ??{ ????MessageBox(NULL,TEXT("Click!"),TEXT("Silverlight?for?Hello?World!!!"),MB_OK); ????return?S_OK; ??} }; #endif //?============================================================================ //??OnLoaded // //??Description:?Calls?InitializeComponent?to?bind?member?variables?to?named //???????????????elements,?and?attach?event?handlers?specified?in?XAML // //??Parameters:??pRoot?-?The?root?dependency?object. //?============================================================================ HRESULT?MainPage::OnLoaded(__in?IXRDependencyObject*?pRoot) { ????UNREFERENCED_PARAMETER(pRoot); ????HRESULT?hr?=?InitializeComponent(); ????return?hr; }?//?OnLoaded #pragma?region?GeneratedCode //?============================================================================ //??WARNING:?DO?NOT?EDIT?THIS?ALWAYS-GENERATED?CODE //?============================================================================ HRESULT?MainPage::InitializeComponent() { ????HRESULT?hr?=?E_FAIL; ??m_clickDelegate?=?NULL; ????FindName(L"LayoutRoot",?&m_pLayoutRoot); #if?TEST_BTN_CLICK_PROCESS ??{ ????HRESULT?retCode?=?0; ????BtnEventHandler?handler; ????if(FAILED(retCode?=?FindName(TEXT("HelloWorldBtn"),?&m_MyBtn))) ??????return?-1; ????/* ????指向委托對象的指針并不是一個(gè)智能指針(smart?pointer),我們需要顯式釋放它:clickDelegate->Release(); ????現(xiàn)在未釋放!??!可以仿?OnLoaded?中?AddLoadedEventHandler?的實(shí)現(xiàn)方式。 ????*/ ????if(FAILED(retCode?=?CreateDelegate(&handler,&BtnEventHandler::OnClick,&m_clickDelegate))) ??????return?-1; ????if(FAILED(retCode?=?m_MyBtn->AddClickEventHandler(m_clickDelegate))) ??????return?-1; ??} #endif ????if?(m_pLayoutRoot?&&?m_MyBtn) ????{ ????????hr?=?S_OK; ????} ????return?hr; } //?============================================================================ //??WARNING:?DO?NOT?EDIT?THIS?ALWAYS-GENERATED?CODE //?============================================================================ #pragma?endregion?GeneratedCode
12 App.cpp 和 App.h 的代碼如下:
App.h
#pragma?once #include// //?This?function?pointer?type?is?used?to?declare?a?table?of //?registration?functions?in?App.cpp // typedef?HRESULT?(*PFN_XRCUC_REGISTER)(); //? //?This?is?the?main?application?class. // class?App { public: ????// ????//?Intialize?member?variables?that?cannot?fail?initialization?in?the?constructor. ????// ????App(): ????????m_bInitialized(FALSE), ????????m_nResult(0) ????{ ????} ????// ????//?Destructor ????// ????~App()?{} ????//?Intialize?the?XamlRuntime?API,?a?VisualHost,?and?initialize?the?application ????HRESULT?Initialize(HINSTANCE?hInstance); ????//?Create?the?VisualHost. ????HRESULT?CreateHost(XRWindowCreateParams*?pCreateParams); ????//?Run?the?application?until?the?message?pump?exits. ????HRESULT?Run(); ????//?Register?the?UserControls?implemented?in?this?module ????HRESULT?RegisterUserControls(); ????//?Get?the?host?window?creation?parameters ????HRESULT?GetWindowParameters(XRWindowCreateParams*?pWindowParameters); ????//?Get?the?result?code?to?be?returned?from?WinMain ????int?GetWinMainResultCode(); ????//?Set?the?result?code?to?be?returned?from?WinMain ????void?SetWinMainResultCode(int?nResult); ????//?Get?the?application?HINSTANCE ????static?HINSTANCE?GetHInstance(); ????//?Exit?the?application ????HRESULT?Exit(); ????//?OnStartup?is?called?after?the?visual?host?is?created ????//?and?before?the?message?loop?is?entered ????HRESULT?OnStartup(); ????//?OnExit?is?called?after?the?message?pump?is?exited ????//?and?before?the?visual?host,?and?IXRApplication?are?destroyed. ????HRESULT?OnExit(); ????//?Register?the?resource?dictionary?for?this?application ????HRESULT?InitializeComponent();?? ????//?Gets?the?visual?host?for?this?application ????static?HRESULT?GetVisualHost(IXRVisualHost**?ppHost); ????//?Gets?IXRApplication?for?this?class ????static?HRESULT?GetApplication(IXRApplication?**?ppApp); private: ????//?Sets?the?visual?host?for?this?application ????static?void?SetVisualHost(IXRVisualHost*?pHost); ????//?Sets?IXRApplication?for?this?class ????static?void?SetApplication(IXRVisualHost*?pApp); protected: ????//?member?variables ????BOOL????????????????????????????m_bInitialized;?//?Initialization?succeeded ????int?????????????????????????????m_nResult;??????//?WinMain?result?code ????//?static?member?variables ????static?HINSTANCE????????????????m_hInstance;????//?The?HINSTANCE?of?this?process ??//?為指向運(yùn)行?SilverLight?應(yīng)用程序的單體對象,此對象用來加載管理分析?XAML?文件。 ????static?IXRApplicationPtr????????m_pApplication;?//?IXRApplication?for?this?process ??//?指向?Windows(HWND)?容器對象對象樹,以便在運(yùn)行時(shí)用?C++?或?XAML?創(chuàng)建的對象處理相應(yīng)事件消息,并顯示或隱藏其?XAML?或?C++?類創(chuàng)建的窗口。 ????static?IXRVisualHostPtr?????????m_pVisualHost;??//?IXRVisualHost?for?this?process }; //?============================================================================ //??Initialize //? //??Description:?Intialize?the?XamlRuntime?API,?and?the?XRApplication?and?then? //???????????????create?a?visual?host. // //??Parameters:??hInstance?-?The?HINSTANCE?from?WinMain //?============================================================================ inline?HRESULT?App::Initialize(HINSTANCE?hInstance) { ????HRESULT?hr?=?E_FAIL; ??//?創(chuàng)建主窗口并讓?SE?管理它的內(nèi)容 ????XRWindowCreateParams?WindowParameters?=?{0}; ????m_hInstance?=?hInstance; ??//?Public?API?exported?from?XamlRumtime.dll.?Initialize?the?system. ????BOOL?m_bInitialized?=?XamlRuntimeInitialize(); ????//?Create?IXRApplication?instance ????if?(m_bInitialized) ????{ ????//?Public?API?exported?from?XR.dll.?Obtain?a?reference?to?the?XRApplication?object?singleton. ????????hr?=?GetXRApplicationInstance(&m_pApplication); ????} ????if?(SUCCEEDED(hr)) ????{ ????//?Add?this?module?for?the?XamlRuntime?to?use?when?automatically?resolving?Image?Source?URIs?as?presented?in?XAML. ????????hr?=?m_pApplication->AddResourceModule(m_hInstance);???? ????} ????if?(SUCCEEDED(hr)) ????{ ????????hr?=?RegisterUserControls(); ????} ????if?(SUCCEEDED(hr)) ????{ ????????hr?=?InitializeComponent(); ????} ????if?(SUCCEEDED(hr)) ????{ ????????hr?=?GetWindowParameters(&WindowParameters); ????} ????if?(SUCCEEDED(hr)) ????{ ????????hr?=?CreateHost(&WindowParameters); ????} ????if?(SUCCEEDED(hr)) ????{ ????????hr?=?OnStartup(); ????} ????return?hr; }?//?Initialize //?============================================================================ //??Run //? //??Description:?Run?the?application?until?the?message?pump?exits. //?============================================================================ inline?HRESULT?App::Run() { ????HRESULT?hr?=?E_FAIL; ????UINT?uiExitCode?=?0; ????if?(m_pVisualHost?!=?NULL) ????{ ????????//?save?the?exit?code?for?WinMain ????????hr?=?m_pVisualHost->StartDialog(&uiExitCode); ????????SetWinMainResultCode(uiExitCode);?? ????} ????//?Allow?user?to?cleanup?resources. ????OnExit(); ????// ????//?XamlRuntime?interfaces?must?be?released?in?the? ????//?following?order:?IXRVisualHost,?IXRApplication. ????//?After?these?interfaces?are?released?the?runtime ????//?can?be?uninitialized. ????// ????//?First?release?IXRVisualHost ????m_pVisualHost?=?NULL; ????//?Second?release?IXRApplication ????m_pApplication?=?NULL; ????//?If?XamlRuntime?was?initialized,?uninitialize?it ????if?(m_bInitialized) ????{ ????????m_bInitialized?=?FALSE; ????????XamlRuntimeUninitialize(); ????} ????m_hInstance=NULL; ????return?hr; }?//?Run //?============================================================================ //??GetWinMainResultCode //? //??Description:?Get?the?result?code?to?be?returned?from?WinMain //?============================================================================ inline?int?App::GetWinMainResultCode() {? ????return?m_nResult;? }?//?GetWinMainResultCode //?============================================================================ //??SetWinMainResultCode //? //??Description:?Set?the?result?code?to?be?returned?from?WinMain // //??Parameters:??nResult?-?The?result?code?to?be?returned?from?WinMain //?============================================================================ inline?void?App::SetWinMainResultCode(int?nResult) {? ????m_nResult?=?nResult;? }?//?SetWinMainResultCode //?============================================================================ //??GetHInstance //? //??Description:?Get?the?application?HINSTANCE //?============================================================================ inline?HINSTANCE?App::GetHInstance() {? ????return?m_hInstance;? }?//?GetHInstance //?============================================================================ //??Exit //? //??Description:?Exit?the?application //?============================================================================ inline?HRESULT?App::Exit()? { ????HRESULT?hr?=?E_FAIL; ????if?(NULL?!=?m_pVisualHost) ????{ ????????hr?=?m_pVisualHost->EndDialog(0); ????} ????return?hr;? }?//?Exit //?============================================================================ //?GetVisualHost // //?Gets?the?visual?host?for?this?application //?============================================================================ inline?HRESULT?App::GetVisualHost(IXRVisualHost?**?ppHost) { ????if?(!ppHost) ????????return?E_INVALIDARG; ????if?(m_pVisualHost) ????{ ????????*ppHost?=?m_pVisualHost; ????????(*ppHost)->AddRef(); ????????return?S_OK; ????} ????return?E_FAIL; }? //?============================================================================ //?SetVisualHost // //?Sets?the?visual?host?for?this?application //?============================================================================ inline?void?App::SetVisualHost(IXRVisualHost*?pHost) { ????//?Smart?pointer?automatically?calls?AddRef ????m_pVisualHost?=?pHost; } //?============================================================================ //?GetApplication // //?Gets?IXRApplication?for?this?class //?============================================================================ inline?HRESULT?App::GetApplication(IXRApplication?**?ppApp) { ????HRESULT?hr?=?E_FAIL; ????if?(!ppApp) ??????return?E_INVALIDARG; ????if?(m_pApplication) ????{ ????????*ppApp?=?m_pApplication; ????????(*ppApp)->AddRef(); ????????hr?=?S_OK; ????} ????return?hr; } //?============================================================================ //?SetApplication // //?Sets?IXRApplication?for?this?class //?============================================================================ inline?void?App::SetApplication(IXRVisualHost*?pApp) { ????//?Smart?pointer?automatically?calls?AddRef ????m_pApplication?=?pApp; }
App.cpp
#include?"stdafx.h" #include?"SilverlightHelloGenerated.h" #include?"App.h" #include?"MainPage.h" //?The?MAX_LOADSTRING?constant?needs?to?be?equal?to?or?greater //?than?the?length?of?the?string?referenced?by?IDS_APP_TITLE #define?MAX_LOADSTRING?100 //?============================================================================ //?Static?class?member?instantiation. //?============================================================================ HINSTANCE?App::m_hInstance;?????????????????//?HINSTANCE?of?this?process IXRApplicationPtr?App::m_pApplication;??????//?IXRApplication?for?this?process IXRVisualHostPtr?App::m_pVisualHost;????????//?IXRVisualHost?for?this?process //?============================================================================ //??InitializeComponent //? //??Description:?Load?the?Application?resource?dictionary?if?one?exists. //?============================================================================ HRESULT?App::InitializeComponent() { ????XRXamlSource?appXaml(GetHInstance(),?IDR_SILVERLIGHTCLOCK_APP); ????HRESULT?hr?=?m_pApplication->LoadResourceDictionary(&appXaml,NULL); ????return?hr; }?//?InitializeComponent //?============================================================================ //??GetWindowParameters //? //??Description:?Set?the?window?creation?parameters?for?this?application. // //??Parameters:??pWindowParameters?-?Window?creation?parameters. //?============================================================================ HRESULT?App::GetWindowParameters(XRWindowCreateParams*?pWindowParameters) { ????static?WCHAR?szTitle[MAX_LOADSTRING];????????//?title?bar?text ????HRESULT?hr?=?E_INVALIDARG; ????if?(pWindowParameters) ????{ ????????pWindowParameters->Style???????=?WS_VISIBLE; ????????pWindowParameters->ExStyle?????=?WS_EX_TOPMOST; ????????//?Set?the?title?bar?text ????????LoadString(m_hInstance,?IDS_APP_TITLE,?szTitle,?MAX_LOADSTRING);? ????????pWindowParameters->pTitle??????=?szTitle; ????????//?Set?window?position ????????pWindowParameters->Left????????=?0; ????????pWindowParameters->Top?????????=?0; ????????//?TODO:?To?specify?a?window?size?for?the?visual?host?set?Width?and?Height ????????//?If?Width?and?Height?are?zero?the?Width?and?Height?specified?in?the ????????//?XAML?are?used ????????//pWindowParameters->Width???????=?GetSystemMetrics(SM_CXSCREEN); ????????//pWindowParameters->Height??????=?GetSystemMetrics(SM_CYSCREEN); ????????hr?=?S_OK; ????} ????return?hr; }?//?GetWindowParameters //?============================================================================ //??OnStartup //? //??Description:?OnStartup?is?called?after?the?visual?host?is?created. //???????????????and?before?the?message?loop?is?entered. //?============================================================================ HRESULT?App::OnStartup() { ????HRESULT?hr?=?S_OK; ????IXRFrameworkElementPtr?pRoot; ????hr?=?m_pVisualHost->GetRootElement(&pRoot); ????if?(SUCCEEDED(hr)) ????{ ????????//?TODO:?Add?one?time?initialization?code?here. ????} ????return?hr; }?//?OnStartup //?============================================================================ //??OnExit //? //??Description:?OnExit?is?called?after?the?message?pump?is?exited //???????????????and?before?the?visual?host,?and?IXRApplication?are?destroyed. //?============================================================================ HRESULT?App::OnExit() { ????//?TODO:?Add?one-time?cleanup?code?here. ????return?S_OK; }?//?OnExit //?============================================================================ //??CreateHost //? //??Description:?Create?the?visual?host. // //??Parameters:??pCreateParams?-?The?parameters?used?for?creating?the? //???????????????visual?host's?window //?============================================================================ HRESULT?App::CreateHost(XRWindowCreateParams*?pCreateParams) { ????XRPtrpControl; ????HRESULT?hr?=?E_FAIL; ????hr?=?m_pApplication->CreateObject(__uuidof(MainPage),&pControl); ????if?(SUCCEEDED(hr)) ????{ ????????hr?=?m_pApplication->CreateHostFromElementTree(pControl,?pCreateParams,?&m_pVisualHost); ????} ????return?hr; } #pragma?region?RegisterUserControls?Generated?Code //?============================================================================ //??RegisterUserControls //? //??Description:?Register?all?XRCustomUserControl?implemenations?here. // //??WARNING:?DO?NOT?EDIT?THIS?ALWAYS-GENERATED?FUNCTION //?============================================================================ HRESULT?App::RegisterUserControls() { ????HRESULT?hr?=?S_OK; ????static?PFN_XRCUC_REGISTER?pfn[]?=? ????{ ????????&MainPage::Register, ????}; ????for?(int?i=0;?i<_countof(pfn)?&&?SUCCEEDED(hr);?i++) ????{ ????????hr?=?pfn[i](); ????????if?(FAILED(hr)) ????????{ ????????????RETAILMSG(1,(L"RegisterUserControls?failed.")); ????????} ????} ???? ????return?hr; }?//?RegisterUserControls //?============================================================================ //??WARNING:?DO?NOT?EDIT?THIS?ALWAYS-GENERATED?FUNCTION //?============================================================================ #pragma?endregion?RegisterUserControls?Generated?Code
13 編譯、運(yùn)行,失敗了!程序沒有加載起來。一般來說,沒有加載成功是因?yàn)?XMAL 解析失敗,或資源沒有加載成功。需要進(jìn)一步分析是什么具體的原因?
調(diào)試代碼發(fā)現(xiàn),如下函數(shù)在加載資源時(shí)報(bào)錯(cuò): hr = -2147023082 {找不到映像文件中指定的資源名。}
HRESULT?App::InitializeComponent() { ????XRXamlSource?appXaml(GetHInstance(),?IDR_SILVERLIGHTCLOCK_APP); ????HRESULT?hr?=?m_pApplication->LoadResourceDictionary(&appXaml,NULL); ????return?hr; }?//?InitializeComponent
解決方法:
將資源文件 SilverlightHelloWorld.rc 中的 SilverlightHelloWorld.rc2 替換成 SilverlightHelloGenerated.rc2,共兩處。
14 編譯、運(yùn)行,可以看到想要的界面。點(diǎn)擊按鍵,會彈出一個(gè)對話框。
OK,到此新建 WinCE 下 Silverlight 工程的整個(gè)過程算是結(jié)束了,是不是有些復(fù)雜?
呵呵...,個(gè)人的感覺也是,挺復(fù)雜的。