Silverlight For WinEmbedded 的页面切换实现

本文章的基础是:新建 WinCE7.0 下的 Silverlight 工程(http://www.cnblogs.com/91program/p/5201231.html)

前一段时间在研究 Silverlight 应用在 Windows Embdeed 下的使用。还在 Silverlight 论坛发帖子求助页面切换的问题,最后没有答案(帖子见:http://bbs.csdn.net/topics/390832361)。
在 Windows Embedded 下只能使用 C++ 配合 Silverlight 使用,但在 PC 上一般使用的是 C#,包括 Windows Phone 的开发,前期也只支持 C#。从 Windows Phone 8.1 开发,好像是支持 C++了。但这方面的资料太少!
建立一个基于 C++ 的 Silverlight 工程,还是比较麻烦的。特别是对 XAML 的处理,一般建议是在 Microsoft Expression Blend 3 中完成。
在安装 Silverlight for Windows Embedded 后,在新建项目中会多一个模板:Silverlight for Winodws Embedded Application,可以使用这个模板来创建基于 Silverlight 的应用。按向导一步步执行,最关键的一步是选择使用 Blend 创建的包含 XAML 的工程和默认的启动页面。
创建成功的 Demo,偶已经上传到 CSDN 下载频道。下载地址如下:http://download.csdn.net/detail/91program/7745251
页面切换的方法是以 MainPage 中的 Grid 为基础,在 Grid 中载入其它的 xaml 资源。即先显示 MainPage页面(一般为有一个或多个 Grid 的空页面),然后根据加载条件,载入不同的页面。

关键的代码和 XAML 如下:
1. MainPage.cpp 

 1 HRESULT MainPage::InitializeComponent()  
 2 {  
 3   HRESULT hr = E_FAIL;  
 4   m_clickDelegate = NULL;  
 5   
 6   
 7   FindName(L"LayoutRoot", &m_pLayoutRoot);  
 8   FindName(L"Gird_Layer_Source", &m_pGird_Layer_Source);  
 9   
10   
11   if (m_pLayoutRoot && m_pGird_Layer_Source)  
12   {  
13     hr = S_OK;  
14   }  
15   
16   
17   return hr;  
18 }  

2.MainPage.xaml

<UserControl  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  xmlns:local="clr-namespace:WinEmbeddedHelloWorld"  
  x:Class="WinEmbeddedHelloWorld.MainPage"  
  Width="800" Height="480">  
  
  
  <Grid x:Name="LayoutRoot" Background="Black">  
  
  
    <Grid x:Name="Gird_Layer_Source" Margin="0,0,0,0">  
    </Grid>  
      
  </Grid>  
</UserControl>  

3. App.cpp 
1) 页面注册

 1 HRESULT App::RegisterUserControls()  
 2 {  
 3     HRESULT hr = S_OK;  
 4   
 5   
 6     static PFN_XRCUC_REGISTER pfn[] =   
 7     {  
 8         &MainPage::Register,  
 9         &SecondPage::Register,  
10     };  
11   
12   
13     for (int i=0; i<_countof(pfn) && SUCCEEDED(hr); i++)  
14     {  
15         hr = pfn[i]();  
16   
17   
18         if (FAILED(hr))  
19         {  
20             RETAILMSG(1,(L"RegisterUserControls failed."));  
21         }  
22   
23   
24     }  
25       
26     return hr;  
27 } // RegisterUserControls  

4. App.cpp 页面切换功能的实现
1) 全局变量

1 IXRGridPtr gpMainLayer = NULL;  

2) 赋值

 1 HRESULT App::CreateHost(XRWindowCreateParams* pCreateParams)  
 2 {  
 3   XRPtr<IXRCustomUserControl>  pControl;  
 4   HRESULT hr = E_FAIL;  
 5   
 6   
 7   hr = m_pApplication->CreateObject(__uuidof(MainPage),&pControl);  
 8   if (SUCCEEDED(hr))  
 9   {  
10     hr = m_pApplication->CreateHostFromElementTree(pControl, pCreateParams, &m_pVisualHost);  
11   }  
12   
13   
14   IXRApplication *pApp = NULL;  
15   App::GetApplication(&pApp);  
16   IXRCustomUserControl *p1 = (IXRCustomUserControl *)pControl;  
17   MainPage *pMainPage = dynamic_cast<MainPage *>(p1);  
18   gpMainLayer = pMainPage->m_pGird_Layer_Source;         // 记录显示的基础 Grid  
19   pMainPage = NULL;  
20   
21   
22   return hr;  
23 }  

3) 测试页面切换

  1. HRESULT App::OnStartup()  
  2. {  
  3.   HRESULT hr = S_OK;  
  4.   IXRFrameworkElementPtr pRoot;  
  5.   
  6.   
  7.   hr = m_pVisualHost->GetRootElement(&pRoot);  
  8.   if (SUCCEEDED(hr))  
  9.   {  
  10.     // TODO: Add one time initialization code here.  
  11.   }  
  12.   
  13.   
  14.   // Leo 测试页面显示, 即页面切换(从 MainPage 切换到 SecondPage)  
  15.   if(NULL != gpMainLayer)  
  16.   {  
  17.     XRPtr<SecondPage> pSwitchPage = NULL;  
  18.     IXRUIElementCollection *pChildList = NULL;  
  19.     gpMainLayer->GetChildren(&pChildList);  
  20.     if(NULL == pChildList)  
  21.     {  
  22.       return NULL;  
  23.     }  
  24.     {  
  25.       HRESULT hr = m_pApplication->CreateObject(__uuidof(SecondPage),&pSwitchPage);  
  26.       if(SUCCEEDED(hr) && NULL != pSwitchPage)  
  27.       {  
  28.         // pSwitchPage->OnLoad();  
  29.         pChildList->Add(pSwitchPage,NULL);  
  30.       }  
  31.     }  
  32.   }  
  33.   
  34.   
  35.   return hr;  
  36. } // OnStartup  

5 SecondPage.xaml

<UserControl  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  mc:Ignorable="d"  
  x:Class="WinEmbeddedHelloWorld.SecondPage"  
  d:DesignWidth="640" d:DesignHeight="480" Width="800">  
  
  
  <Grid x:Name="LayoutRoot" Background="Green">  
    <Button Height="38" HorizontalAlignment="Left" Margin="65,54,0,0" VerticalAlignment="Top" Width="177" Content="Second Page"/>  
    <Button Height="38" HorizontalAlignment="Left" Margin="65,117,0,0" VerticalAlignment="Top" Width="177" Content="Origin Prj"/>  
  </Grid>  
    
</UserControl>  
原文地址:https://www.cnblogs.com/91program/p/5201191.html