您的位置:首页 > 移动开发

在Wince 中使用IXRResourceDictionary加载全局资源App.xaml

2010-07-24 13:39 375 查看
   阅读本DEMO假定您对Silverlight,WinCe C++(本人初学)开发有一定了解。由于涉及到一些项目的隐私效果图就不上了,参考资料http://msdn.microsoft.com/en-us/library/ee503954.aspx
1、创建SDK:这个比较简单找到先前我们创建编译成功的的OS项目在SDKs目录右键添加新的SDK设置相关属性,编译SDK成功后,安装生成的**SDK.msi至开发机器便可针对此版本SDK进行相关应用开发!
2、使用安装的SDK建立项目,开发wince应用
   2.1:建立项目:
     2.1.1:新建一个Win32智能设备项目



     2.1.2:选择我们自己编译的SDK“SDK_Silverlight”



     2.1.3:新建一个空白windows应用程



     2.1.4:添加项目运行时依赖xamlruntime.lib



     2.1.5:启动SDK模拟器设置Storage card共享目录以方便进行调试和效果查看



3、在Blend中设计和准备用到的XAML资源
 3.1:设计按纽样式及全局画刷资源App.XAML参考:

<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
<Application.Resources>
<!-- Resources scoped at the Application level should be defined here. -->
<ImageBrush x:Key="imageBrushMainBg"  ImageSource="image/mainBg.png"/>
<ImageBrush x:Key="imageBrushMainTitle" ImageSource="image/mainTitle.png"/>
<ImageBrush x:Key="imageBrushMainButtom" ImageSource="image/mainBottom.png"/>
<ImageBrush x:Key="imageMenuButton" ImageSource="image/menu_0.png"/>
<Style x:Key="imageButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid RenderTransformOrigin="0.5,0.5" x:Name="grid">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="FocusStates">
<vsm:VisualStateGroup.Transitions>
<vsm:VisualTransition GeneratedDuration="00:00:00.6000000" To="Focused"/>
<vsm:VisualTransition GeneratedDuration="00:00:00" To="Unfocused"/>
</vsm:VisualStateGroup.Transitions>
<vsm:VisualState x:Name="Unfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Focused">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
<SplineDoubleKeyFrame KeyTime="00:00:00.200000" Value="0.9"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
<SplineDoubleKeyFrame KeyTime="00:00:00.200000" Value="0.9"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
<vsm:VisualStateGroup x:Name="CommonStates">
<vsm:VisualStateGroup.Transitions>
<vsm:VisualTransition GeneratedDuration="00:00:00.4000000" To="Normal"/>
<vsm:VisualTransition GeneratedDuration="00:00:00.6000000" To="Pressed"/>
</vsm:VisualStateGroup.Transitions>
<vsm:VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#26605151"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.9"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.9"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="Disabled"/>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Rectangle Height="Auto" Margin="-15,-15,-15,-15" VerticalAlignment="Stretch" Stroke="{x:Null}" Visibility="Visible" x:Name="rectangle" Fill="#FF000000"/>
<Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto" Grid.Row="2" Fill="{StaticResource imageMenuButton}" Stroke="{x:Null}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
 
  3.2:设计Page.XAML主页面参考:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800" Height="600">
<Grid x:Name="LayoutRoot" Background="{StaticResource imageBrushMainBg}">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition/>
<RowDefinition Height="105" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto" Grid.Row="2" Fill="{StaticResource imageBrushMainButtom}" Stroke="{x:Null}" x:Name="rectMainBg"/>
<StackPanel HorizontalAlignment="Center" Margin="0,0,0,0" Width="Auto" Grid.Row="2" VerticalAlignment="Center" Background="{x:Null}" x:Name="panelMainButton" Orientation="Horizontal">
<Button Height="101" Style="{StaticResource imageButton}" mce_Style="{StaticResource imageButton}" Width="97" Content="Button"/>
<Image x:Name="img1" Height="101" Width="97" Source="IMAGE/MENU_1.PNG" Stretch="Fill" Margin="15,0,15,0"/>
<Image x:Name="img2" Height="101" Width="97" Source="IMAGE/MENU_2.PNG" Stretch="Fill" Margin="15,0,15,0"/>
<Button Height="101" Style="{StaticResource imageButton}" mce_Style="{StaticResource imageButton}" Width="97" Content="Button"/>
<Button Height="101" Style="{StaticResource imageButton}" mce_Style="{StaticResource imageButton}" Width="97" Content="Button"/>
</StackPanel>

</Grid>
</UserControl>
 
 4、详细C++代码实现(资源可以使用外部和嵌入式两种我采取了前者外部加载资源的方式):
   4.1:设置一些资源数据全局变量
UINT exitCode;
IXRVisualHostPtr vhost;
LPCTSTR strFilePath[8];
IXRBitmapImagePtr images[8];
IXRFrameworkElement* pRoot = NULL;
IXRApplication *app;
 
   4.2:初使化我们会使用到的图片资源路径信息:
//初始化图片资源信息数组
void InitPicturePathStr(void)
{
strFilePath[0] = L"//Storage Card//image//mainBg.png";
strFilePath[1] = L"//Storage Card//image//mainTitle.png";
strFilePath[2] = L"//Storage Card//image//mainBottom.png";
strFilePath[3] = L"//Storage Card//image//menu_0.png";
strFilePath[4] = L"//Storage Card//image//menu_1.png";
strFilePath[5] = L"//Storage Card//image//menu_2.png";
strFilePath[6] = L"//Storage Card//image//returnMain.png";
strFilePath[7] = L"//Storage Card//image//lightSetNol.png";
}
 
   4.3:加载我们会使用到的图片资源

//动态加载图片资源
void SetAppImageResouece(HINSTANCE hInstance,IXRApplication* app)
{
HRESULT rc;
for(int i=0;i<8;i++)
{
if(FAILED(rc = app ->CreateObject(IID_IXRBitmapImage,&images[i]))) break;
if(FAILED(rc =images[i] ->SetUriSource(strFilePath[i])))
{
break;
}
}
}
 
    4.4:为Application加载全局资源App.XAML
     
IXRResourceDictionary *pResourceDictionary;
XRXamlSource source;
source.SetFile(L"//Storage Card//App.xaml");

app->LoadResourceDictionary(&source,&pResourceDictionary);
 
    4.5:为App.xaml中的ImageBrush设置图片信息

app->GetResourceDictionary(&pResourceDictionary);
IXRImageBrush* imageBrush;
for(int i=0;i<4;i++)
{
switch (i)
{
case 0:
pResourceDictionary->GetItem(TEXT("imageBrushMainBg"),&imageBrush);
imageBrush->SetImageSource(images[0]);
break;
case 1:
pResourceDictionary->GetItem(TEXT("imageBrushMainTitle"),&imageBrush);
imageBrush->SetImageSource(images[1]);
break;
case 2:
pResourceDictionary->GetItem(TEXT("imageBrushMainButtom"),&imageBrush);
imageBrush->SetImageSource(images[2]);
break;
case 3:
pResourceDictionary->GetItem(TEXT("imageMenuButton"),&imageBrush);
imageBrush->SetImageSource(images[3]);
break;
}
}
 
     4.6:设置窗体样式加载主页面Page.xaml文件

XRWindowCreateParams wp;
ZeroMemory(&wp,sizeof(XRWindowCreateParams));
wp.Style = WS_POPUP;
wp.pTitle =L"Scheme";
wp.Left = 0;
wp.Top = 0;
wp.Width = 800;
wp.Height = 600;

XRXamlSource xamlSrc;
xamlSrc.SetFile(L"//Storage Card//Page.xaml");
//xamlSrc.SetResource(hInstance,L"XAML",MAKEINTRESOURCE(500));

if(FAILED(retcode = app ->CreateHostFromXaml(&xamlSrc,&wp,&vhost))) return -1;
vhost ->GetRootElement(&pRoot);
 
   4.7:为Page.xaml中使用到的图片控件设置图片信息

IXRImage* curImage;
for(int j=0;j<2;j++)
{
switch(j)
{
case 0:
if(!FAILED(pRoot->FindName(TEXT("img1"),&curImage)))
curImage->SetSource(images[5]);
break;
case 1:
if(!FAILED(pRoot->FindName(TEXT("img2"),&curImage)))
curImage->SetSource(images[4]);
break;
}
//AttachMouseEvent(curImage);
}
 
   4.8:显示窗体在模拟器上运行:
 
if(FAILED(retcode =vhost->StartDialog(&exitCode))) return -1;

本文结束,下一篇将介绍一下如何注册鼠标事件实现图片按钮的拖拽和缩放。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息