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

来源:互联网 发布:中国网安 知乎 编辑:程序博客网 时间:2024/05/21 06:30
阅读本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智能设备项目

http://hi.csdn.net/attachment/201007/24/2243869_12799366977Cf0.jpg
     2.1.2:选择我们自己编译的SDK“SDK_Silverlight”

http://hi.csdn.net/attachment/201007/24/2243869_12799366988ZvZ.jpg
     2.1.3:新建一个空白windows应用程

http://hi.csdn.net/attachment/201007/24/2243869_1279936698j22e.jpg
     2.1.4:添加项目运行时依赖xamlruntime.lib

http://hi.csdn.net/attachment/201007/24/2243869_1279936699y373.jpg
     2.1.5:启动SDK模拟器设置Storage card共享目录以方便进行调试和效果查看

http://hi.csdn.net/attachment/201007/24/2243869_1279936700Dk4D.jpg

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

[c-sharp] view plaincopy
  1. <Application  
  2.  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">  
  4.  <Application.Resources>  
  5.   <!-- Resources scoped at the Application level should be defined here. -->  
  6.   <ImageBrush x:Key="imageBrushMainBg"  ImageSource="image/mainBg.png"/>  
  7.   <ImageBrush x:Key="imageBrushMainTitle" ImageSource="image/mainTitle.png"/>  
  8.   <ImageBrush x:Key="imageBrushMainButtom" ImageSource="image/mainBottom.png"/>  
  9.   <ImageBrush x:Key="imageMenuButton" ImageSource="image/menu_0.png"/>  
  10.   <Style x:Key="imageButton" TargetType="Button">  
  11.    <Setter Property="Template">  
  12.     <Setter.Value>  
  13.      <ControlTemplate TargetType="Button">  
  14.       <Grid RenderTransformOrigin="0.5,0.5" x:Name="grid">  
  15.        <Grid.RenderTransform>  
  16.         <TransformGroup>  
  17.          <ScaleTransform/>  
  18.          <SkewTransform/>  
  19.          <RotateTransform/>  
  20.          <TranslateTransform/>  
  21.         </TransformGroup>  
  22.        </Grid.RenderTransform>  
  23.        <vsm:VisualStateManager.VisualStateGroups>  
  24.         <vsm:VisualStateGroup x:Name="FocusStates">  
  25.          <vsm:VisualStateGroup.Transitions>  
  26.           <vsm:VisualTransition GeneratedDuration="00:00:00.6000000" To="Focused"/>  
  27.           <vsm:VisualTransition GeneratedDuration="00:00:00" To="Unfocused"/>  
  28.          </vsm:VisualStateGroup.Transitions>  
  29.          <vsm:VisualState x:Name="Unfocused">  
  30.           <Storyboard>  
  31.            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.Visibility)">  
  32.             <DiscreteObjectKeyFrame KeyTime="00:00:00">  
  33.              <DiscreteObjectKeyFrame.Value>  
  34.               <Visibility>Collapsed</Visibility>  
  35.              </DiscreteObjectKeyFrame.Value>  
  36.             </DiscreteObjectKeyFrame>  
  37.            </ObjectAnimationUsingKeyFrames>  
  38.           </Storyboard>  
  39.          </vsm:VisualState>  
  40.          <vsm:VisualState x:Name="Focused">  
  41.           <Storyboard>  
  42.            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">  
  43.             <SplineDoubleKeyFrame KeyTime="00:00:00.200000" Value="0.9"/>  
  44.            </DoubleAnimationUsingKeyFrames>  
  45.            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">  
  46.             <SplineDoubleKeyFrame KeyTime="00:00:00.200000" Value="0.9"/>  
  47.            </DoubleAnimationUsingKeyFrames>  
  48.           </Storyboard>  
  49.          </vsm:VisualState>  
  50.         </vsm:VisualStateGroup>  
  51.         <vsm:VisualStateGroup x:Name="CommonStates">  
  52.          <vsm:VisualStateGroup.Transitions>  
  53.           <vsm:VisualTransition GeneratedDuration="00:00:00.4000000" To="Normal"/>  
  54.           <vsm:VisualTransition GeneratedDuration="00:00:00.6000000" To="Pressed"/>  
  55.          </vsm:VisualStateGroup.Transitions>  
  56.          <vsm:VisualState x:Name="MouseOver">  
  57.           <Storyboard>  
  58.            <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">  
  59.             <SplineColorKeyFrame KeyTime="00:00:00" Value="#26605151"/>  
  60.            </ColorAnimationUsingKeyFrames>  
  61.           </Storyboard>  
  62.          </vsm:VisualState>  
  63.          <vsm:VisualState x:Name="Normal">  
  64.           <Storyboard>  
  65.            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.Visibility)">  
  66.             <DiscreteObjectKeyFrame KeyTime="00:00:00">  
  67.              <DiscreteObjectKeyFrame.Value>  
  68.               <Visibility>Collapsed</Visibility>  
  69.              </DiscreteObjectKeyFrame.Value>  
  70.             </DiscreteObjectKeyFrame>  
  71.            </ObjectAnimationUsingKeyFrames>  
  72.           </Storyboard>  
  73.          </vsm:VisualState>  
  74.          <vsm:VisualState x:Name="Pressed">  
  75.           <Storyboard>  
  76.            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">  
  77.             <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.9"/>  
  78.            </DoubleAnimationUsingKeyFrames>  
  79.            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="grid" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">  
  80.             <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.9"/>  
  81.            </DoubleAnimationUsingKeyFrames>  
  82.           </Storyboard>  
  83.          </vsm:VisualState>  
  84.          <vsm:VisualState x:Name="Disabled"/>  
  85.         </vsm:VisualStateGroup>  
  86.        </vsm:VisualStateManager.VisualStateGroups>  
  87.        <Rectangle Height="Auto" Margin="-15,-15,-15,-15" VerticalAlignment="Stretch" Stroke="{x:Null}" Visibility="Visible" x:Name="rectangle" Fill="#FF000000"/>  
  88.        <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto" Grid.Row="2" Fill="{StaticResource imageMenuButton}" Stroke="{x:Null}" />  
  89.       </Grid>  
  90.      </ControlTemplate>  
  91.     </Setter.Value>  
  92.    </Setter>  
  93.   </Style>  
  94.  </Application.Resources>  
  95. </Application>  
 
  3.2:设计Page.XAML主页面参考:
[c-sharp] view plaincopy
  1. <UserControl  
  2.  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.  Width="800" Height="600">  
  5.  <Grid x:Name="LayoutRoot" Background="{StaticResource imageBrushMainBg}">  
  6.   <Grid.RowDefinitions>  
  7.    <RowDefinition Height="30" />  
  8.    <RowDefinition/>  
  9.    <RowDefinition Height="105" />  
  10.   </Grid.RowDefinitions>  
  11.   <Grid.ColumnDefinitions>  
  12.    <ColumnDefinition />  
  13.   </Grid.ColumnDefinitions>  
  14.   <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto" Grid.Row="2" Fill="{StaticResource imageBrushMainButtom}" Stroke="{x:Null}" x:Name="rectMainBg"/>  
  15.   <StackPanel HorizontalAlignment="Center" Margin="0,0,0,0" Width="Auto" Grid.Row="2" VerticalAlignment="Center" Background="{x:Null}" x:Name="panelMainButton" Orientation="Horizontal">  
  16.    <Button Height="101" Style="{StaticResource imageButton}" mce_Style="{StaticResource imageButton}" Width="97" Content="Button"/>  
  17.    <Image x:Name="img1" Height="101" Width="97" Source="IMAGE/MENU_1.PNG" Stretch="Fill" Margin="15,0,15,0"/>  
  18.    <Image x:Name="img2" Height="101" Width="97" Source="IMAGE/MENU_2.PNG" Stretch="Fill" Margin="15,0,15,0"/>  
  19.    <Button Height="101" Style="{StaticResource imageButton}" mce_Style="{StaticResource imageButton}" Width="97" Content="Button"/>  
  20.    <Button Height="101" Style="{StaticResource imageButton}" mce_Style="{StaticResource imageButton}" Width="97" Content="Button"/>  
  21.   </StackPanel>  
  22.     
  23.  </Grid>  
  24. </UserControl>  
 
 4、详细C++代码实现(资源可以使用外部和嵌入式两种我采取了前者外部加载资源的方式):
   4.1:设置一些资源数据全局变量
[cpp] view plaincopy
  1. UINT exitCode;  
  2. IXRVisualHostPtr vhost;  
  3. LPCTSTR strFilePath[8];  
  4. IXRBitmapImagePtr images[8];  
  5. IXRFrameworkElement* pRoot = NULL;  
  6. IXRApplication *app;  
 
   4.2:初使化我们会使用到的图片资源路径信息:
[cpp] view plaincopy
  1. //初始化图片资源信息数组  
  2. void InitPicturePathStr(void)  
  3. {  
  4.  strFilePath[0] = L"//Storage Card//image//mainBg.png";  
  5.  strFilePath[1] = L"//Storage Card//image//mainTitle.png";  
  6.  strFilePath[2] = L"//Storage Card//image//mainBottom.png";  
  7.  strFilePath[3] = L"//Storage Card//image//menu_0.png";  
  8.  strFilePath[4] = L"//Storage Card//image//menu_1.png";  
  9.  strFilePath[5] = L"//Storage Card//image//menu_2.png";  
  10.  strFilePath[6] = L"//Storage Card//image//returnMain.png";  
  11.  strFilePath[7] = L"//Storage Card//image//lightSetNol.png";  
  12. }  
 
   4.3:加载我们会使用到的图片资源
[cpp] view plaincopy
  1. //动态加载图片资源  
  2. void SetAppImageResouece(HINSTANCE hInstance,IXRApplication* app)  
  3. {  
  4.  HRESULT rc;  
  5.  for(int i=0;i<8;i++)  
  6.  {  
  7.   if(FAILED(rc = app ->CreateObject(IID_IXRBitmapImage,&images[i]))) break;  
  8.   if(FAILED(rc =images[i] ->SetUriSource(strFilePath[i])))  
  9.   {   
  10.    break;  
  11.   }  
  12.  }  
  13. }  
 
    4.4:为Application加载全局资源App.XAML
     
[cpp] view plaincopy
  1. IXRResourceDictionary *pResourceDictionary;  
  2.  XRXamlSource source;  
  3.  source.SetFile(L"//Storage Card//App.xaml");  
  4.   
  5.  app->LoadResourceDictionary(&source,&pResourceDictionary);  
 
    4.5:为App.xaml中的ImageBrush设置图片信息
[cpp] view plaincopy
  1. app->GetResourceDictionary(&pResourceDictionary);  
  2.  IXRImageBrush* imageBrush;  
  3.  for(int i=0;i<4;i++)  
  4.  {  
  5.   switch (i)  
  6.   {  
  7.   case 0:  
  8.    pResourceDictionary->GetItem(TEXT("imageBrushMainBg"),&imageBrush);  
  9.    imageBrush->SetImageSource(images[0]);  
  10.    break;  
  11.   case 1:  
  12.    pResourceDictionary->GetItem(TEXT("imageBrushMainTitle"),&imageBrush);  
  13.    imageBrush->SetImageSource(images[1]);  
  14.    break;  
  15.   case 2:  
  16.    pResourceDictionary->GetItem(TEXT("imageBrushMainButtom"),&imageBrush);  
  17.    imageBrush->SetImageSource(images[2]);  
  18.    break;  
  19.   case 3:  
  20.    pResourceDictionary->GetItem(TEXT("imageMenuButton"),&imageBrush);  
  21.    imageBrush->SetImageSource(images[3]);  
  22.    break;  
  23.   }  
  24.  }  
 
     4.6:设置窗体样式加载主页面Page.xaml文件
[cpp] view plaincopy
  1. XRWindowCreateParams wp;  
  2. ZeroMemory(&wp,sizeof(XRWindowCreateParams));  
  3. wp.Style = WS_POPUP;  
  4. wp.pTitle =L"Scheme";  
  5. wp.Left = 0;  
  6. wp.Top = 0;  
  7. wp.Width = 800;  
  8. wp.Height = 600;  
  9.   
  10. XRXamlSource xamlSrc;  
  11. xamlSrc.SetFile(L"//Storage Card//Page.xaml");  
  12. //xamlSrc.SetResource(hInstance,L"XAML",MAKEINTRESOURCE(500));  
  13.   
  14. if(FAILED(retcode = app ->CreateHostFromXaml(&xamlSrc,&wp,&vhost))) return -1;  
  15. vhost ->GetRootElement(&pRoot);  
 
   4.7:为Page.xaml中使用到的图片控件设置图片信息
[cpp] view plaincopy
  1. IXRImage* curImage;  
  2.  for(int j=0;j<2;j++)  
  3.  {  
  4.   switch(j)  
  5.   {  
  6.   case 0:  
  7.    if(!FAILED(pRoot->FindName(TEXT("img1"),&curImage)))  
  8.     curImage->SetSource(images[5]);  
  9.    break;  
  10.   case 1:  
  11.    if(!FAILED(pRoot->FindName(TEXT("img2"),&curImage)))  
  12.     curImage->SetSource(images[4]);  
  13.    break;  
  14.   }  
  15.   //AttachMouseEvent(curImage);  
  16.  }  
 
   4.8:显示窗体在模拟器上运行:
 
[cpp] view plaincopy
  1. if(FAILED(retcode =vhost->StartDialog(&exitCode))) return -1; 
原创粉丝点击