当前位置:首页 > 芯闻号 > 充电吧
[导读]这也是我写此文章时一步一步建立的 Silverlight 工程。1 使用 Microsoft Expression Blend 3 创建一个 Silverlight for Windows Embed

这也是我写此文章时一步一步建立的 Silverlight 工程。

1 使用 Microsoft Expression Blend 3 创建一个 Silverlight for Windows Embedded Application 工程,放一个按键控件在窗体上,命名按键然后保存。由于 Microsoft Expression Blend 3 现在只支持生成 C# 的代码,对我们没有什么用,所以我们只用其中的 XAML 文件。
2 VS2008 新建:智能设备->Win32 智能设备项目,选拔一个支持 Silverlight 的SDK。
3 文件 SilverlightHelloWorld.cpp 中,只保留如下代码,其它删除:

// SilverlightHelloWorld.cpp : 定义应用程序的入口点。
//


#include "stdafx.h"
#include "SilverlightHelloWorld.h"




int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
  return 0;
}


4 新增加 4 个空文件,内容后面补,分别是: 

    App.cpp
    App.h
    MainPage.cpp
    MainPage.h


5 增加文件 XRPack.rules,文件的内容如下:  



6 将 XRPack.rules 增加到工程文件 SilverlightHelloWorld.vcproj 中,修改了两个地方,修改后的内容如下:

......


7 新增文件 SilverlightHelloWorld.xrpack,其内容如下:

# 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 ,增加以下内容:

// 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() 函数的实现代码如下

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 编译,是可以成功的,但还需要根据 XAML 文件的内容来修改 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;
    /*
    指向委托对象的指针并不是一个智能指针(smart pointer),我们需要显式释放它:clickDelegate->Release();
    现在未释放!!!可以仿 OnLoaded 中 AddLoadedEventHandler 的实现方式。
    */
    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
  // 为指向运行 SilverLight 应用程序的单体对象,此对象用来加载管理分析 XAML 文件。
    static IXRApplicationPtr        m_pApplication; // IXRApplication for this process
  // 指向 Windows(HWND) 容器对象对象树,以便在运行时用 C++ 或 XAML 创建的对象处理相应事件消息,并显示或隐藏其 XAML 或 C++ 类创建的窗口。
    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;


  // 创建主窗口并让 SE 管理它的内容
    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 编译、运行,失败了!程序没有加载起来。一般来说,没有加载成功是因为 XMAL 解析失败,或资源没有加载成功。需要进一步分析是什么具体的原因?
调试代码发现,如下函数在加载资源时报错: 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 编译、运行,可以看到想要的界面。点击按键,会弹出一个对话框。
OK,到此新建 WinCE 下 Silverlight 工程的整个过程算是结束了,是不是有些复杂?
呵呵...,个人的感觉也是,挺复杂的。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

6月21日消息,最近在法国巴黎举行的联合国教科文组织首届阿勒福赞奖颁奖仪式上,中国科学院古脊椎动物与古人类研究所付巧妹获得阿勒福赞科学、技术、工程、数学领域杰出青年科学家国际奖(简称"阿勒福赞奖")。

关键字: 科学 技术 工程

PID是一种非常经典的控制类算法,凭着它的简单易用在工程上得到了广泛的应用,并且影响力也是极高,那为什么说其简单易用呢?可以说只要你对PID的主要的参数对系统的影响理解得足够好,完全可以通过手动试凑的方式来获得一套合适的...

关键字: PID 控制类算法 工程

过去五年间中国超算服务市场的年复合增长率保持在24。%左右,预计至2025年,这一数据将继续维持在24.1%。继武汉超算中心项目本月封顶后,7月22日,国内首个超算互联网工程在山东济南上线。

关键字: 超算 互联网 工程

摘要:运用“以工作过程为导向”的高职课程开发模式,给出了深入开展物联网工程实施与管理课程的建设与教学设计结果。该模式可以保证行动导向的教学方法所实施的实际效果。

关键字: 物联网 高职院校 课程建设 工程

现在你应该知道我们写的c语言程序,需要借助于编译器编译成机器能够看懂的机器文件,也就是hex文件。同时你也应该知道这个hex文件所处的文件目录在哪里。

关键字: PIC16F887 MPLAB IDE 工程

近日,据外媒报道,科普期刊《科学美国人》与世界经济论坛组织领先技术专家组成的国际指导小组,评选出2019年的“十大新兴技术”。 1.环境:生物塑料可以解决严重污染问题 根据世界经济论坛的数据,仅201

关键字: 塑料 工程 新能源 机器人 环境 生物技术 计算 元透镜 食品跟踪

大家都说我们国家是“基建狂魔”,奇迹般的基建工程也着实不少,但在一些超高难度项目中,即便是基建狂魔也倍感头疼。 不断延伸的全国高铁网络中,地质条件复杂的长隧道不在少数,比如7分钟车程但足足挖了13年、

关键字: 工程 铁路 隧道 拉林铁路 巴玉

9月25日,平潭海峡公铁两用大桥实现全部合龙,标志着世界在建难度最大桥梁工程、世界最长跨海峡公铁大桥、我国第一座跨海峡公铁大桥“平潭海峡公铁大桥”全桥贯通,预计2020年全面通车。据悉,平潭海峡公铁大

关键字: 大桥 工程 桥梁 贯通 跨海 平潭海峡 公铁两用

2011年9月8日,云南省综合应急救援总队与中国移动通信集团云南有限公司举行了云南省应急救援光缆网暨移动通信网络工程战略合作协议签字仪式,正式启动云南省应急救援通信指挥光缆网和移动通信网络工程建设。

关键字: 光缆 工程 通信网络
关闭
关闭