原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

[源码下载]

重新想象 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的更多相关文章

  1. 重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding

    原文:重新想象 Windows 8 Store Apps (14) - 控件 UI: RenderTransform, Projection, Clip, UseLayoutRounding [源码下 ...

  2. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  3. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  4. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

  5. 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示

    原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...

  6. 重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView

    原文:重新想象 Windows 8 Store Apps (11) - 控件之 ListView 和 GridView [源码下载] 重新想象 Windows 8 Store Apps (11) - ...

  7. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

  8. 重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础

    原文:重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础 [源码下载] 重新想象 Windows 8 Store Apps (9) - 控件之 Sc ...

  9. 重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGrid, VariableSizedWrapGrid

    原文:重新想象 Windows 8 Store Apps (7) - 控件之布局控件: Canvas, Grid, StackPanel, VirtualizingStackPanel, WrapGr ...

随机推荐

  1. Linux多线程服务端编程:使用muduo C++网络库

    内容推荐本 书主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread.这 ...

  2. SDUT 1304-取数字问题(DFS)

    取数字问题 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 给定M×N的矩阵,当中的每一个元素都是-10到10之间的整数.你的 ...

  3. [SVN]创建本地的SVN仓库

    本地创建SVN仓库,就算是自己平时写代码也养成使用SVN的习惯. 环境: OS:Mac OS X10.9.1 SVN Version:1.7.10 创建本地SVN仓库: $svnadmin creat ...

  4. 事务应用-运行多条SQL语句

    事务具有原子性,要么不运行,要么全运行,一旦成功运行永久保存.而这些正是因为事务的原子性和对数据库的持久性形成的.下面是一个关于统一给数据库中的数据改动的批量操作,利用到事务. TODO:批量改动数据 ...

  5. Java程序猿之从菜鸟到职场高手的必看

    J2SE之入门引导            Java基础系列之初识JAVA                                           Java基础系列之Java语法       ...

  6. FZU 1686(重复覆盖)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31370 题意:用尽量少r*c的小矩形覆盖大矩形n*m中的所有1,将 ...

  7. COLORREF和COLOR和RGB的总结

    一.COLORREF与RGB的相互转化 RGB(r,g,b)是一个宏 实际上它做得事是((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((D ...

  8. ACM-简单题之Least Common Multiple——hdu1019

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  9. Net分布式系统

    Net分布式系统 Net分布式系统之三:Keepalived+LVS+Nginx负载均衡之高可用 摘要: 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡, ...

  10. java文件创建、删除、读取、写入操作大全

    一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{ System.out.println("请输入您的 ...