重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager
作者:webabcd
介绍
重新想象 Windows 8 Store Apps 之 控件 UI
- 字体继承 - 继承父辈的 Font 相关的信息
- Style - 样式
- ControlTemplate - 控件模板
- 系统资源 - 系统内置的样式资源
- VisualState - 视图状态
- VisualStateManager - 视图状态管理器
示例
1、演示字体继承
Controls/UI/FontInherit.xaml
<Page
x:Class="XamlDemo.Controls.UI.FontInherit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontSize="100"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <!--
演示如何继承父辈的 Font 相关的信息
Font 相关的设置来自 Windows.UI.Xaml.Controls.Control
--> <!--
继承了 Page 的关于 Font 的设置
-->
<TextBlock Text="FontSize = 100" /> <UserControl FontSize="50">
<!--
继承了 UserControl 的关于 Font 的设置
-->
<TextBlock Text="FontSize = 50" />
</UserControl> </StackPanel>
</Grid>
</Page>
2、演示 Style
Controls/UI/Style.xaml
<Page
x:Class="XamlDemo.Controls.UI.Style"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Name="root" Background="Transparent"> <!--
注:
1、在 Grid.Resources 指定的资源,其作用域仅在 Grid 之内,全局资源需要在 App.xaml 中配置
2、Grid.Resources 等非全局资源也是支持 ResourceDictionary 的
-->
<Grid.Resources> <!--
Style - 样式
x:Key - 标识(不指定此值,则样式会应用于所有 TargetType 所指定的类型)
TargetType - 目标对象类型
BasedOn - 指定当前样式的父样式(此样式会继承指定的父样式)
Setter - 属性设置器
Property - 需要设置的属性名称
Value - 需要设置的属性值
--> <Style x:Key="MyTextStyle" TargetType="TextBox">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="#0000FF"/>
</Style> <Style x:Key="MyTextStyle2" TargetType="TextBox">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="#FF0000"/>
</Style> <Style TargetType="TextBox" BasedOn="{StaticResource MyTextStyle}">
<Setter Property="TextAlignment" Value="Center"/>
</Style>
</Grid.Resources> <StackPanel Margin="120 0 0 0"> <!--通过指定样式资源,修改 FrameworkElement 的样式(Style 属性来自 FrameworkElement)-->
<TextBox Name="txtStyleDemo" Text="我是 TextBox" Margin="5" Style="{StaticResource MyTextStyle}" /> <!--隐式样式(即全局样式,即样式资源中未指定 key 的样式)的应用-->
<TextBox Text="我是 TextBox" Margin="5" /> <!--动态改变 FrameworkElement 的样式-->
<Button Name="btnChangeStyle" Content="改变样式" Click="btnChangeStyle_Click_1" /> </StackPanel>
</Grid>
</Page>
Controls/UI/Style.xaml.cs
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls.UI
{
public sealed partial class Style : Page
{
public Style()
{
this.InitializeComponent();
} private void btnChangeStyle_Click_1(object sender, RoutedEventArgs e)
{
// 获取 Application 中的资源
// (Windows.UI.Xaml.Style)Application.Current.Resources["myStyle"]; // 获取 root 内的资源
txtStyleDemo.Style = (Windows.UI.Xaml.Style)root.Resources["MyTextStyle2"];
}
}
}
3、演示 ControlTemplate
Controls/UI/ControlTemplate.xaml
<Page
x:Class="XamlDemo.Controls.UI.ControlTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Name="root" Background="Transparent"> <!--
注:
1、在 Grid.Resources 指定的资源,其作用域仅在 Grid 之内,全局资源需要在 App.xaml 中配置
2、Grid.Resources 等非全局资源也是支持 ResourceDictionary 的
-->
<Grid.Resources> <!--
ControlTemplate - 控件模板
x:Key - 标识
TargetType - 目标对象类型
ContentPresenter - 用于显示 ContentControl 中的 Content
TemplateBinding - 模板绑定
--> <ControlTemplate x:Key="MyControlTemplate" TargetType="Button">
<Border BorderBrush="Red" BorderThickness="1">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Right" Foreground="Red" />
</Grid>
</Border>
</ControlTemplate> <ControlTemplate x:Key="MyControlTemplate2" TargetType="Button">
<Border BorderBrush="Red" BorderThickness="1">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Right" Foreground="Blue" />
</Grid>
</Border>
</ControlTemplate> <!--在 Style 内配置 ControlTemplate-->
<Style x:Key="MyStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Red" BorderThickness="1">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Right" Foreground="Red" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources> <StackPanel Margin="120 0 0 0"> <!--通过指定控件模板资源,修改 Control 的模板(Template 属性来自 Control)-->
<Button Name="btnControlTemplateDemo" Width="300" Margin="5" Content="我是 Button" Background="Yellow" Template="{StaticResource MyControlTemplate}" /> <!--通过内联控件模板,修改 Control 的模板-->
<Button Width="300" Margin="5" Content="我是 Button">
<Button.Template>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<Grid Background="Yellow">
<ContentPresenter HorizontalAlignment="Right" Foreground="Red" />
</Grid>
</Border>
</ControlTemplate>
</Button.Template>
</Button> <!--在 Style 内配置 ControlTemplate-->
<Button Width="300" Margin="5" Content="我是 Button" Background="Yellow" Style="{StaticResource MyStyle}" /> <!--动态改变 Control 的模板-->
<Button Name="btnChangeControlTemplate" Content="改变控件模板" Click="btnChangeControlTemplate_Click_1" /> </StackPanel>
</Grid>
</Page>
Controls/UI/ControlTemplate.xaml.cs
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls.UI
{
public sealed partial class ControlTemplate : Page
{
public ControlTemplate()
{
this.InitializeComponent();
} private void btnChangeControlTemplate_Click_1(object sender, RoutedEventArgs e)
{
// 获取 Application 中的资源
// (Windows.UI.Xaml.Style)Application.Current.Resources["MyControlTemplate"]; // 获取 root 内的资源
btnControlTemplateDemo.Template = (Windows.UI.Xaml.Controls.ControlTemplate)root.Resources["MyControlTemplate2"];
}
}
}
4、演示如何使用系统内置的样式资源
Controls/UI/SystemResource.xaml
<Page
x:Class="XamlDemo.Controls.UI.SystemResource"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="120 0 0 0"> <!--
有 n 多的系统资源可用,以下举几个例子 注:可以在 Visual Studio 中枚举出系统资源
--> <Border Padding="12,4,12,4"
BorderThickness="{StaticResource ButtonBorderThemeThickness}"
BorderBrush="{StaticResource ButtonBorderThemeBrush}"
Background="{StaticResource ButtonBackgroundThemeBrush}">
<TextBlock Text="我是一个长得像 Button 的 TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="SemiBold"
FontFamily="{StaticResource ContentControlThemeFontFamily}"
FontSize="{StaticResource ControlContentThemeFontSize}"
Foreground="{StaticResource ButtonForegroundThemeBrush}" />
</Border> <Button Content="我是一个 Button" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>
5、演示 VisualState 和 VisualStateManager 的应用
Controls/UI/VisualStateDemo.xaml
<Page
x:Class="XamlDemo.Controls.UI.VisualStateDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Controls.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<Grid.Resources> <ControlTemplate x:Key="myControlTemplate" TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<!--
VisualStateGroup - 用于分组 VisualState
-->
<VisualStateGroup x:Name="CommonStates">
<!--
Normal - 正常状态 注意:
1、本例所列出的 VisualState 名称都是 Button 控件拥有的,不同的控件的 VisualState 名称和种类可能会不一样
2、写自定义控件时,需要通过 VisualStateManager.GoToState() 来转换 VisualState
-->
<VisualState x:Name="Normal" />
<!--
Disabled - 无效状态
-->
<VisualState x:Name="Disabled" />
<!--
PointerOver - 鼠标经过时的状态
-->
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation
Storyboard.TargetName="borderBrush"
Storyboard.TargetProperty="Color"
To="Green" />
</Storyboard>
</VisualState>
<!--
PointerOver - 鼠标按下时的状态
-->
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="grid">
<DiscreteObjectKeyFrame KeyTime="0:0:0.3" Value="{StaticResource ButtonPressedBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentPresenter">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPressedForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<!--
VisualTransition - VisualState 变化时的过渡效果
From - 变化前的 VisualState 的 Name
To - 变化后的 VisualState 的 Name
GeneratedDuration - 一个状态变化到另一个状态的所需时间
GeneratedEasingFunction - 一个状态变化到另一个状态的缓动效果
-->
<VisualStateGroup.Transitions>
<VisualTransition To="PointerOver" GeneratedDuration="0:0:1">
<VisualTransition.GeneratedEasingFunction>
<ElasticEase EasingMode="EaseInOut" />
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<!--
Focused - 获取到焦点
-->
<VisualState x:Name="Focused" />
<!--
Unfocused - 失去焦点
-->
<VisualState x:Name="Unfocused"/>
<!--
PointerFocused - 通过指针获取到焦点
-->
<VisualState x:Name="PointerFocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups> <Border x:Name="border" BorderThickness="10">
<Border.BorderBrush>
<SolidColorBrush x:Name="borderBrush" Color="Red" />
</Border.BorderBrush>
<Grid Name="grid" Background="{TemplateBinding Background}" Width="500" Height="200">
<ContentPresenter Name="contentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="24.667"
Foreground="{TemplateBinding Foreground}" />
</Grid>
</Border>
</Grid>
</ControlTemplate> </Grid.Resources> <StackPanel Margin="120 0 0 0"> <Button Name="btnDemo" Content="我是 Button(用于演示 VisualState)" Margin="5" Background="Blue" Foreground="White" Template="{StaticResource myControlTemplate}" /> <Button Name="btnVisualStateManager" Content="将上面的按钮的 VisualState 转到 PointerOver" Click="btnVisualStateManager_Click_1" Margin="5" /> </StackPanel> </Grid>
</Page>
Controls/UI/VisualStateDemo.xaml.cs
/*
* 演示 VisualState 和 VisualStateManager 的应用
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.Controls.UI
{
public sealed partial class VisualStateDemo : Page
{
public VisualStateDemo()
{
this.InitializeComponent();
} private void btnVisualStateManager_Click_1(object sender, RoutedEventArgs e)
{
/*
* bool GoToState(Control control, string stateName, bool useTransitions) - 转换 VisualState
* control - 需要转换 VisualState 的控件
* stateName - 目标 VisualState 的名称
* useTransitions - 是否使用 VisualTransition 进行过渡
*/ // 将 VisualState 转到指定的状态
VisualStateManager.GoToState(btnDemo, "PointerOver", true);
}
}
}
OK
[源码下载]
重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager的更多相关文章
- 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding
原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...
- 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree
原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...
- 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试
原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...
- 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom
原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...
- 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示
原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...
- 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView
原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...
- 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom
原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...
- 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础
原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...
- 重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid
原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGr ...
随机推荐
- 汇编与高级语言(插图结合Delphi代码,来自linzhengqun)
汇编与高级语言 1. 汇编基础知识 1.1. 寄存器 寄存器 用途 EAX,EBX,EDX,ECX 通用寄存器,由程序员自己指定用途,也有一些不成文的用法: EAX:常用于运算. ...
- iOS8指纹识别TouchID
苹果在2014年6月3日的WWDC2014开幕式上推出了新版iOS8系统,界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好.iOS8通知中心更加强大,支持消息直接回复操作,并支持Quic ...
- [Android学习笔记]子线程更新UI线程方法之Handler
关于此笔记 不讨论: 1.不讨论Handler实现细节 2.不讨论android线程派发细节 讨论: 子线程如何简单的使用Handler更新UI 问题: android开发时,如何在子线程更新UI? ...
- ZooKeeper的安装、配置、启动和使用(一)——单机模式
ZooKeeper的安装.配置.启动和使用(一)——单机模式 ZooKeeper的安装非常简单,它的工作模式分为单机模式.集群模式和伪集群模式,本博客旨在总结ZooKeeper单机模式下如何安装.配置 ...
- 非对称加密RSA、Elgamal、背包算法、Rabin、D-H、ECC(椭圆曲线加密算法)等。使用最广泛的是RSA算法
非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey).公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密:如果用私 ...
- 每个Android开发者必须知道的资源集锦
英文原文:Resources every Android developer must know 随着 Android 平台持续惊人的增长,越来越多的开发人员开始工作于 Android 应用程序.而且 ...
- [poj 2991]Crane[线段树表示向量之和,而非数量]
题意: 起重机的机械臂, 由n段组成, 对某一些连接点进行旋转, 询问每次操作后的末端坐标. 思路: 由于旋转会影响到该点之后所有线段的角度, 因此容易想到用线段树记录角度, 成段更新. (但是不是每 ...
- 百度词典搜索_dress code
百度词典搜索_dress code dress code n.着装标准
- MySQL在一台db服务器上面如何启动多个实例
安装过程省略过,源码安装请参考http://write.blog.csdn.net/postlist/1609043/all 整理自己的文档,发现以前做的例子,share下,欢迎大家提出改进意见. 一 ...
- 基于Cocos2dx开发卡牌游戏Demo_放开那三国 2.0
PS:下载地址在最以下 1.登录 2.副本选择 3.地图 4. 选择敌人 5. 战斗 6. 战斗结算 7. 地图拓展 8. 武将拓展 9. 下载地址: 点击打开链接