查看: 5957|回复: 5

[教程] Win8风格的应用程序开发:资源

[复制链接]
ykdkiller 发表于 2012-10-7 14:21 | 显示全部楼层 |阅读模式
快御云安全
      本人在校学生,正在Win8开发学习中,现在把学习笔记以教程的形式发上来,请各位大侠指教。

      在Win8风格的应用程序开发中,资源是一种有效地代码重用和代码管理的办法。
       具体而言,就是我们可以先自定义一种风格或样式,然后在后面的程序中,我们只须在要用它们的时候进行调用即可。下面就是一个具体的例子:
  1. <Page
  2.         x:Class="Test.MainPage"
  3.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.         xmlns:local="using:Test"
  6.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8.         mc:Ignorable="d">
  9.    
  10.             <Page.Resources>
  11.                     <LinearGradientBrush x:Key="jybrush" StartPoint="0, 0" EndPoint="1, 1">
  12.                             <GradientStop Color="Red" Offset="0.0"/>
  13.                             <GradientStop Color="Yellow" Offset="1.0"/>
  14.                     </LinearGradientBrush>
  15.            </Page.Resources>

  16.             <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
  17.                     <Rectangle Fill="{StaticResource jybrush}"/>
  18.             </Grid>
  19.     </Page>
复制代码
     在这个XAML中,我们定义一个叫“jybrush”的画刷(Brush),它是一个线性的渐变色,从红到黄。然后我们在<Grid>元素中对它进行了调用,效果如下:
as.jpg
      此外,我们还可以建立资源字典,把所有的资源放在一个文件中。下面具体说明一下。
    首先,我们新建一个Test的解决方案,然后在Test的项目上新建一个资源字典文件ResDict.xaml,然后在其中添加资源:
  1. <ResourceDictionary
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:local="using:Test">

  5.             <LinearGradientBrush x:Key="jybrush" StartPoint="0, 0" EndPoint="1, 1">
  6.                     <GradientStop Color="Red" Offset="0.0"/>
  7.                     <GradientStop Color="Yellow" Offset="1.0"/>
  8.             </LinearGradientBrush>
  9.     </ResourceDictionary>
复制代码
     这样我们就可以在MainPage.xaml中对它进行调用:
  1. <Page.Resources>
  2.             <ResourceDictionary>
  3.                     <ResourceDictionary.MergedDictionaries>
  4.                             <ResourceDictionary Source="ResDict.xaml"/>    这里就是所要调用的资源
  5.                     </ResourceDictionary.MergedDictionaries>
  6.             </ResourceDictionary>
  7.     </Page.Resources>

  8.     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
  9.             <Rectangle Fill="{StaticResource jybrush}"/>
  10.     </Grid>
复制代码
     它实现了一样的效果。




qiaohuobaba 发表于 2012-10-8 08:37 | 显示全部楼层
好多代码啊
回复

使用道具 举报

chengang1992 发表于 2012-10-16 21:18 | 显示全部楼层
很简单的。。就是普通的 xaml,会silverlight的都会这个。。
回复

使用道具 举报

hzqo0o0o0 发表于 2012-10-17 11:14 | 显示全部楼层
跟开发wp7应用很像啊~!
回复

使用道具 举报

大感谢1 发表于 2024-7-28 16:35 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

1260035050 发表于 2025-3-5 18:37 | 显示全部楼层
谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋手机版联系我们

Copyright © 2005-2025 PCBeta. All rights reserved.

Powered by Discuz!  CDN加速及安全服务由「快御」提供

请勿发布违反中华人民共和国法律法规的言论,会员观点不代表远景论坛官方立场。

远景在线 ( 苏ICP备17027154号 )|远景论坛 |Win11论坛 |Win10论坛 |Win8论坛 |Win7论坛 |WP论坛 |Office论坛

GMT+8, 2025-3-14 13:41

快速回复 返回顶部 返回列表