本文章的基础是:新建 WinCE7.0 下的 Silverlight 工程(
http://blog.csdn.net/91program/article/details/36675607)
前一段时间在研究 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
2.MainPage.xaml
3. App.cpp
1) 页面注册
4. App.cpp 页面切换功能的实现
1) 全局变量
2) 赋值
3) 测试页面切换
5 SecondPage.xaml
前一段时间在研究 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
HRESULT MainPage::InitializeComponent()
{
HRESULT hr = E_FAIL;
m_clickDelegate = NULL;
FindName(L"LayoutRoot", &m_pLayoutRoot);
FindName(L"Gird_Layer_Source", &m_pGird_Layer_Source);
if (m_pLayoutRoot && m_pGird_Layer_Source)
{
hr = S_OK;
}
return hr;
}
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) 页面注册
HRESULT App::RegisterUserControls()
{
HRESULT hr = S_OK;
static PFN_XRCUC_REGISTER pfn[] =
{
&MainPage::Register,
&SecondPage::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
4. App.cpp 页面切换功能的实现
1) 全局变量
IXRGridPtr gpMainLayer = NULL;
2) 赋值
HRESULT App::CreateHost(XRWindowCreateParams* pCreateParams)
{
XRPtr<IXRCustomUserControl> pControl;
HRESULT hr = E_FAIL;
hr = m_pApplication->CreateObject(__uuidof(MainPage),&pControl);
if (SUCCEEDED(hr))
{
hr = m_pApplication->CreateHostFromElementTree(pControl, pCreateParams, &m_pVisualHost);
}
IXRApplication *pApp = NULL;
App::GetApplication(&pApp);
IXRCustomUserControl *p1 = (IXRCustomUserControl *)pControl;
MainPage *pMainPage = dynamic_cast<MainPage *>(p1);
gpMainLayer = pMainPage->m_pGird_Layer_Source; // 记录显示的基础 Grid
pMainPage = NULL;
return hr;
}
3) 测试页面切换
HRESULT App::OnStartup()
{
HRESULT hr = S_OK;
IXRFrameworkElementPtr pRoot;
hr = m_pVisualHost->GetRootElement(&pRoot);
if (SUCCEEDED(hr))
{
// TODO: Add one time initialization code here.
}
// Leo 测试页面显示, 即页面切换(从 MainPage 切换到 SecondPage)
if(NULL != gpMainLayer)
{
XRPtr<SecondPage> pSwitchPage = NULL;
IXRUIElementCollection *pChildList = NULL;
gpMainLayer->GetChildren(&pChildList);
if(NULL == pChildList)
{
return NULL;
}
{
HRESULT hr = m_pApplication->CreateObject(__uuidof(SecondPage),&pSwitchPage);
if(SUCCEEDED(hr) && NULL != pSwitchPage)
{
// pSwitchPage->OnLoad();
pChildList->Add(pSwitchPage,NULL);
}
}
}
return hr;
} // 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>