- UID
- 2402090
- 最后登录
- 1970-1-1
- 阅读权限
- 20
- 精华
- 主题
- 回帖
- 0
- 积分
- 66
- PB币
-
- 威望
-
- 贡献
-
- 技术
-
- 活跃
-
|

本人在校学生,正在Win8开发学习中,现在把学习笔记以教程的形式发上来,请各位大侠指教。
在Win8风格的应用程序开发中,资源是一种有效地代码重用和代码管理的办法。
具体而言,就是我们可以先自定义一种风格或样式,然后在后面的程序中,我们只须在要用它们的时候进行调用即可。下面就是一个具体的例子:
- <Page
- x:Class="Test.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:Test"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
-
- <Page.Resources>
- <LinearGradientBrush x:Key="jybrush" StartPoint="0, 0" EndPoint="1, 1">
- <GradientStop Color="Red" Offset="0.0"/>
- <GradientStop Color="Yellow" Offset="1.0"/>
- </LinearGradientBrush>
- </Page.Resources>
- <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
- <Rectangle Fill="{StaticResource jybrush}"/>
- </Grid>
- </Page>
复制代码 在这个XAML中,我们定义一个叫“jybrush”的画刷(Brush),它是一个线性的渐变色,从红到黄。然后我们在<Grid>元素中对它进行了调用,效果如下:
此外,我们还可以建立资源字典,把所有的资源放在一个文件中。下面具体说明一下。
首先,我们新建一个Test的解决方案,然后在Test的项目上新建一个资源字典文件ResDict.xaml,然后在其中添加资源:
- <ResourceDictionary
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:Test">
- <LinearGradientBrush x:Key="jybrush" StartPoint="0, 0" EndPoint="1, 1">
- <GradientStop Color="Red" Offset="0.0"/>
- <GradientStop Color="Yellow" Offset="1.0"/>
- </LinearGradientBrush>
- </ResourceDictionary>
复制代码 这样我们就可以在MainPage.xaml中对它进行调用:
- <Page.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary Source="ResDict.xaml"/> 这里就是所要调用的资源
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- </Page.Resources>
- <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
- <Rectangle Fill="{StaticResource jybrush}"/>
- </Grid>
复制代码 它实现了一样的效果。
|
|