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

本文介绍如何在WinCE平台上创建并使用自定义SDK进行Silverlight应用开发的过程,包括项目搭建、XAML资源设计及C++代码实现等关键步骤。
阅读本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.youkuaiyun.com/attachment/201007/24/2243869_12799366977Cf0.jpg
     2.1.2:选择我们自己编译的SDK“SDK_Silverlight”

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

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

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

http://hi.youkuaiyun.com/attachment/201007/24/2243869_1279936700Dk4D.jpg

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

[c-sharp] view plain copy
  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 plain copy
  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:设置一些资源数据全局变量
  1. UINT exitCode;  
  2. IXRVisualHostPtr vhost;  
  3. LPCTSTR strFilePath[8];  
  4. IXRBitmapImagePtr images[8];  
  5. IXRFrameworkElement* pRoot = NULL;  
  6. IXRApplication *app;  
 
   4.2:初使化我们会使用到的图片资源路径信息:
  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:加载我们会使用到的图片资源
  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
     
  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设置图片信息
  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文件
  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中使用到的图片控件设置图片信息
  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:显示窗体在模拟器上运行:
 
  1. if(FAILED(retcode =vhost->StartDialog(&exitCode))) return -1; 
【语音分离】基于平均谐波结构建模的无监督单声道音乐声源分离(Matlab代码实现)内容概要:本文介绍了基于平均谐波结构建模的无监督单声道音乐声源分离方法,并提供了相应的Matlab代码实现。该方法通过对音乐信号中的谐波结构进行建模,利用音源间的频率特征差异,实现对混合音频中不同乐器或人声成分的有效分离。整个过程无需标注数据,属于无监督学习范畴,适用于单通道录音场景下的语音与音乐分离任务。文中强调了算法的可复现性,并附带完整的仿真资源链接,便于读者学习与验证。; 适合人群:具备一定信号处理基础和Matlab编程能力的高校学生、科研人员及从事音频处理、语音识别等相关领域的工程师;尤其适合希望深入理解声源分离原理并进行算法仿真实践的研究者。; 使用场景及目标:①用于音乐音频中人声与伴奏的分离,或不同乐器之间的分离;②支持无监督条件下的语音处理研究,推动盲源分离技术的发展;③作为学术论文复现、课程项目开发或科研原型验证的技术参考。; 阅读建议:建议读者结合提供的Matlab代码与网盘资料同步运行调试,重点关注谐波建模与频谱分解的实现细节,同时可扩展学习盲源分离中的其他方法如独立成分分析(ICA)或非负矩阵分解(NMF),以加深对音频信号分离机制的理解。
内容概要:本文系统介绍了新能源汽车领域智能底盘技术的发展背景、演进历程、核心技术架构及创新形态。文章指出智能底盘作为智能汽车的核心执行层,通过线控化(X-By-Wire)和域控化实现驱动、制动、转向、悬架的精准主动控制,支撑高阶智能驾驶落地。技术发展历经机械、机电混合到智能三个阶段,当前以线控转向、线控制动、域控制器等为核心,并辅以传感器、车规级芯片、功能安全等配套技术。文中还重点探讨了“智能滑板底盘”这一创新形态,强调其高度集成化、模块化优势及其在成本、灵活性、空间利用等方面的潜力。最后通过“2025智能底盘先锋计划”的实车测试案例,展示了智能底盘在真实场景中的安全与性能表现,推动技术从研发走向市场验证。; 适合人群:汽车电子工程师、智能汽车研发人员、新能源汽车领域技术人员及对智能底盘技术感兴趣的从业者;具备一定汽车工程或控制系统基础知识的专业人士。; 使用场景及目标:①深入了解智能底盘的技术演进路径与系统架构;②掌握线控技术、域控制器、滑板底盘等关键技术原理与应用场景;③为智能汽车底盘研发、系统集成与技术创新提供理论支持与实践参考。; 阅读建议:建议结合实际车型和技术标准进行延伸学习,关注政策导向与行业测试动态,注重理论与实车验证相结合,全面理解智能底盘从技术构想到商业化落地的全过程。
【顶级EI复现】计及连锁故障传播路径的电力系统 N-k 多阶段双层优化及故障场景筛选模型(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI复现】计及连锁故障传播路径的电力系统 N-k 多阶段双层优化及故障场景筛选模型(Matlab代码实现)》的技术资源,重点围绕电力系统中连锁故障的传播路径展开研究,提出了一种N-k多阶段双层优化模型,并结合故障场景筛选方法,用于提升电力系统在复杂故障条件下的安全性与鲁棒性。该模型通过Matlab代码实现,具备较强的工程应用价值和学术参考意义,适用于电力系统风险评估、脆弱性分析及预防控制策略设计等场景。文中还列举了大量相关的科研技术支持方向,涵盖智能优化算法、机器学习、路径规划、信号处理、电力系统管理等多个领域,展示了广泛的仿真与复现能力。; 适合人群:具备电力系统、自动化、电气工程等相关背景,熟悉Matlab编程,有一定科研基础的研究生、高校教师及工程技术人员。; 使用场景及目标:①用于电力系统连锁故障建模与风险评估研究;②支撑高水平论文(如EI/SCI)的模型复现与算法验证;③为电网安全分析、故障传播防控提供优化决策工具;④结合YALMIP等工具进行数学规划求解,提升科研效率。; 阅读建议:建议读者结合提供的网盘资源,下载完整代码与案例进行实践操作,重点关注双层优化结构与场景筛选逻辑的设计思路,同时可参考文档中提及的其他复现案例拓展研究视野。
### WPF 中 App.xaml.cs 定义的变量是否为全局变量 在 WPF 应用程序中,`App.xaml.cs` 文件中的定义可以通过特定方式实现全局访问的效果。以下是详细说明: #### 1. **App.xaml.cs 的作用** `App.xaml.cs` 是 WPF 应用程序的入口点之一,它继承自 `System.Windows.Application` 类。该文件通常用于初始化应用程序资源、处理全局事件(如 `Application_Startup` 和 `Application_Exit`)以及定义全局可用的对象或变量[^5]。 #### 2. **定义全局变量的方式** 在 `App.xaml.cs` 中定义的变量可以通过以下方式实现全局访问: - **静态属性**:将变量定义为静态属性,使其在整个应用程序生命周期内均可访问。 ```csharp public partial class App : Application { public static string GlobalVariable { get; set; } } ``` 通过这种方式定义的变量可以在任何地方通过 `App.GlobalVariable` 访问[^3]。 - **实例属性**:如果需要非静态的全局变量,可以利用 `Application.Current` 属性访问当前应用程序实例。 ```csharp public partial class App : Application { public string InstanceVariable { get; set; } } ``` 在其他地方可以通过以下方式访问该变量: ```csharp var value = ((App)Application.Current).InstanceVariable; ``` 这种方法利用了 `Application.Current` 的全局可达性,从而实现对实例属性的访问[^3]。 #### 3. **全局变量的限制** 尽管可以通过上述方法实现全局变量的效果,但需要注意以下几点: - **生命周期管理**:全局变量的生命周期与应用程序一致,因此需要确保其值在适当的时间进行初始化和清理。 - **线程安全性**:如果多个线程可能同时访问或修改全局变量,需要考虑线程安全问题。 - **设计模式**:过度依赖全局变量可能导致代码耦合度增加,建议结合单例模式或其他设计模式优化全局状态管理。 #### 4. **示例代码** 以下是一个完整的示例,展示如何在 `App.xaml.cs` 中定义全局变量并在其他地方使用: ##### App.xaml.cs: ```csharp public partial class App : Application { public static string StaticGlobalVariable { get; set; } = "Static Value"; public string InstanceGlobalVariable { get; set; } = "Instance Value"; } ``` ##### 其他地方访问: ```csharp // 访问静态全局变量 string staticValue = App.StaticGlobalVariable; // 访问实例全局变量 string instanceValue = ((App)Application.Current).InstanceGlobalVariable; ``` #### 5. **与 App.xaml 的关系** `App.xaml` 文件主要用于定义应用程序级资源和样式。例如,在 `App.xaml` 中定义的资源可以通过 `Application.Resources` 在整个应用程序中共享[^1]。这些资源与 `App.xaml.cs` 中定义的变量类似,都具有全局可达性。 ##### 示例: ```xml <Application x:Class="WpfApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <Style TargetType="Button"> <Setter Property="Background" Value="LightBlue"/> </Style> </Application.Resources> </Application> ``` 上述定义的按钮样式将在整个应用程序中生效。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值