[源码下载]

背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI

作者:webabcd

介绍
背水一战 Windows 10 之 控件 UI

  • VisualState 和 VisualStateManager
  • 控件的默认 Style, ControlTemplate, VisualState

示例
1、演示“VisualState 和 VisualStateManager”相关知识点
Controls/UI/VisualState/VisualStateDemo.xaml

  1. <Page
  2. x:Class="Windows10.Controls.UI.VisualState.VisualStateDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.UI.VisualState"
  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. <Grid Background="Transparent">
  11. <Grid.Resources>
  12.  
  13. <!--
  14. 在 ControlTemplate 中定义 VisualState 和 VisualStateManager
  15. -->
  16. <ControlTemplate x:Key="ControlTemplate1" TargetType="Button">
  17. <Grid>
  18. <VisualStateManager.VisualStateGroups>
  19. <!--
  20. VisualStateGroup - 用于分组 VisualState
  21. -->
  22. <VisualStateGroup x:Name="CommonStates">
  23.  
  24. <!--
  25. Normal - 正常状态
  26.  
  27. 注意:
  28. 1、本例所列出的 VisualState 的名称都是 Button 控件拥有的,不同的控件的 VisualState 名称和种类可能会不一样
  29. 2、写自定义控件时,需要通过 VisualStateManager.GoToState() 来转换 VisualState
  30. -->
  31. <VisualState x:Name="Normal" />
  32.  
  33. <!--
  34. Disabled - 无效状态
  35. -->
  36.  
  37. <VisualState x:Name="Disabled" />
  38.  
  39. <!--
  40. PointerOver - 鼠标经过时的状态(详细的过渡效果在后面的 VisualStateGroup.Transitions 中定义)
  41. -->
  42. <VisualState x:Name="PointerOver">
  43. <Storyboard>
  44. <ColorAnimation
  45. Storyboard.TargetName="borderBrush"
  46. Storyboard.TargetProperty="Color"
  47. To="Green" />
  48. </Storyboard>
  49. </VisualState>
  50.  
  51. <!--
  52. Pressed - 鼠标按下时的状态
  53. -->
  54. <VisualState x:Name="Pressed">
  55. <VisualState.Storyboard>
  56. <Storyboard>
  57. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="grid">
  58. <DiscreteObjectKeyFrame KeyTime="0:0:0.3" Value="{StaticResource ButtonPressedBackgroundThemeBrush}"/>
  59. </ObjectAnimationUsingKeyFrames>
  60. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentPresenter">
  61. <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPressedForegroundThemeBrush}"/>
  62. </ObjectAnimationUsingKeyFrames>
  63. </Storyboard>
  64. </VisualState.Storyboard>
  65. <VisualState.Setters>
  66. <!--
  67. 这部分是 uwp 新增的特性,以前只能通过 Storyboard 来实现
  68. -->
  69. <Setter Target="grid.Width" Value="100" />
  70. </VisualState.Setters>
  71. <VisualState.StateTriggers>
  72. <!--
  73. 这部分是 uwp 新增的特性
  74. 关于 StateTriggers 请参见 /Controls/UI/VisualState/StateTrigger.xaml
  75. -->
  76. </VisualState.StateTriggers>
  77. </VisualState>
  78.  
  79. <!--
  80. VisualTransition - VisualState 变化时的过渡效果(要结合相应的 VisualState 中的 Storyboard 使用)
  81. From - 变化前的 VisualState 的 Name
  82. To - 变化后的 VisualState 的 Name
  83. GeneratedDuration - 一个状态变化到另一个状态的所需时间
  84. GeneratedEasingFunction - 一个状态变化到另一个状态的缓动效果
  85. -->
  86. <VisualStateGroup.Transitions>
  87. <VisualTransition To="PointerOver" GeneratedDuration="0:0:1">
  88. <VisualTransition.GeneratedEasingFunction>
  89. <ElasticEase EasingMode="EaseInOut" />
  90. </VisualTransition.GeneratedEasingFunction>
  91. </VisualTransition>
  92. </VisualStateGroup.Transitions>
  93. </VisualStateGroup>
  94.  
  95. <VisualStateGroup x:Name="MyStates">
  96. <VisualState x:Name="MyState1" />
  97. <VisualState x:Name="MyState2"/>
  98. <VisualState x:Name="MyState3"/>
  99. </VisualStateGroup>
  100.  
  101. </VisualStateManager.VisualStateGroups>
  102.  
  103. <Border x:Name="border" BorderThickness="10">
  104. <Border.BorderBrush>
  105. <SolidColorBrush x:Name="borderBrush" Color="Red" />
  106. </Border.BorderBrush>
  107. <Grid Name="grid" Background="{TemplateBinding Background}" Width="500" Height="200">
  108. <ContentPresenter Name="contentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="24.667"
  109. Foreground="{TemplateBinding Foreground}" />
  110. </Grid>
  111. </Border>
  112. </Grid>
  113. </ControlTemplate>
  114.  
  115. </Grid.Resources>
  116.  
  117. <StackPanel Margin="10 0 10 10">
  118.  
  119. <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5" />
  120.  
  121. <Button Name="btnDemo" Content="我是 Button(用于演示 VisualState 和 VisualStateManager)" Margin="5" Background="Blue" Foreground="White" Template="{StaticResource ControlTemplate1}" />
  122.  
  123. <Button Name="btnVisualStateManager" Content="将上面的按钮的 VisualState 转到 PointerOver" Click="btnVisualStateManager_Click" Margin="5" />
  124.  
  125. </StackPanel>
  126.  
  127. </Grid>
  128. </Page>

Controls/UI/VisualState/VisualStateDemo.xaml.cs

  1. /*
  2. * 演示“VisualState 和 VisualStateManager”相关知识点
  3. */
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows10.Common;
  10.  
  11. namespace Windows10.Controls.UI.VisualState
  12. {
  13. public sealed partial class VisualStateDemo : Page
  14. {
  15. public VisualStateDemo()
  16. {
  17. this.InitializeComponent();
  18. }
  19.  
  20. private void btnVisualStateManager_Click(object sender, RoutedEventArgs e)
  21. {
  22. /*
  23. * bool GoToState(Control control, string stateName, bool useTransitions) - 转换 VisualState
  24. * control - 需要转换 VisualState 的控件
  25. * stateName - 目标 VisualState 的名称
  26. * useTransitions - 是否使用 VisualTransition 进行过渡
  27. */
  28.  
  29. // 将 VisualState 转到指定的状态(每个 VisualStateGroup 分别指定一个其内的 VisualState)
  30. VisualStateManager.GoToState(btnDemo, "PointerOver", true);
  31. VisualStateManager.GoToState(btnDemo, "MyState3", false);
  32.  
  33. /*
  34. * VisualStateManager.GetVisualStateGroups(FrameworkElement obj) - 获取指定 FrameworkElement 中的 VisualStateGroup 集合
  35. * 注:本例中的 VisualState 定义在 btnDemo 的控件模板中的第一个 Grid 中
  36. *
  37. * VisualStateGroup - VisualState 组(每个 VisualStateManager 下可以有多个 VisualStateGroup)
  38. * Name - 获取此 VisualStateGroup 的名字
  39. * CurrentState - 获取此 VisualStateGroup 的当前使用的 VisualState(每个 VisualStateGroup 正在使用的 VisualState 只能有一个)
  40. * States - 获取此 VisualStateGroup 中的 VisualState 集合
  41. * Transitions - 获取此 VisualStateGroup 中的 VisualTransition 集合
  42. * CurrentStateChanging, CurrentStateChanged - 此 VisualStateGroup 中的正在使用的 VisualState 发生变化时触发的事件
  43. *
  44. * VisualState - VisualState
  45. * Name - 获取此 VisualState 的名字
  46. * Setters - 获取此 VisualState 中的 Setter 集合
  47. * StateTriggers - 获取此 VisualState 中的 StateTrigger 集合
  48. * Storyboard - 获取此 VisualState 中的 Storyboard 对象
  49. */
  50.  
  51. lblMsg.Text = "";
  52. Grid grid = Helper.GetVisualChild<Grid>(btnDemo);
  53. IList<VisualStateGroup> visualStateGroups = VisualStateManager.GetVisualStateGroups(grid);
  54. foreach (VisualStateGroup visualStateGroup in visualStateGroups)
  55. {
  56. lblMsg.Text += visualStateGroup.Name + " " + visualStateGroup.CurrentState.Name;
  57. lblMsg.Text += Environment.NewLine;
  58. }
  59. }
  60. }
  61. }

2、演示如何获取控件的默认 Style, ControlTemplate, VisualState
Controls/UI/DefaultUI.xaml

  1. <Page
  2. x:Class="Windows10.Controls.UI.DefaultUI"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.UI"
  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. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <TextBlock TextWrapping="Wrap" Margin="5">
  14. <Run>如何获取控件的默认 Style, ControlTemplate, VisualState 呢?</Run>
  15. <LineBreak />
  16. <Run>1、在 msdn 上找: https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/mt299122.aspx</Run>
  17. <LineBreak />
  18. <Run>2、在 Visual Studio 的设计界面,右键选择控件,然后选择“编辑控件”或“编辑模板”或“编辑其他模板”,然后选择“编辑副本”</Run>
  19. <LineBreak />
  20. <Run>3、打开这个 C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10586.0\Generic\generic.xaml 文件,然后在里面找</Run>
  21. </TextBlock>
  22.  
  23. <TextBlock TextWrapping="Wrap" Margin="5">
  24. <Run>如果遇到复杂的控件,如何确定我要编辑的其内部的控件的类型呢?</Run>
  25. <LineBreak />
  26. <Run>运行你的程序,然后在“实时可视化树(Live Visual Tree)”中找</Run>
  27. </TextBlock>
  28.  
  29. </StackPanel>
  30. </Grid>
  31.  
  32. <Page.Resources>
  33. <!--
  34. 这个就是 Button 的默认样式
  35. -->
  36. <Style x:Key="ButtonStyle1" TargetType="Button">
  37. <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
  38. <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
  39. <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
  40. <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
  41. <Setter Property="Padding" Value="8,4,8,4"/>
  42. <Setter Property="HorizontalAlignment" Value="Left"/>
  43. <Setter Property="VerticalAlignment" Value="Center"/>
  44. <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
  45. <Setter Property="FontWeight" Value="Normal"/>
  46. <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
  47. <Setter Property="UseSystemFocusVisuals" Value="True"/>
  48. <Setter Property="Template">
  49. <Setter.Value>
  50. <ControlTemplate TargetType="Button">
  51. <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
  52. <VisualStateManager.VisualStateGroups>
  53. <VisualStateGroup x:Name="CommonStates">
  54. <VisualState x:Name="Normal">
  55. <Storyboard>
  56. <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
  57. </Storyboard>
  58. </VisualState>
  59. <VisualState x:Name="PointerOver">
  60. <Storyboard>
  61. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
  62. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}"/>
  63. </ObjectAnimationUsingKeyFrames>
  64. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
  65. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
  66. </ObjectAnimationUsingKeyFrames>
  67. <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
  68. </Storyboard>
  69. </VisualState>
  70. <VisualState x:Name="Pressed">
  71. <Storyboard>
  72. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
  73. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}"/>
  74. </ObjectAnimationUsingKeyFrames>
  75. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
  76. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}"/>
  77. </ObjectAnimationUsingKeyFrames>
  78. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
  79. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
  80. </ObjectAnimationUsingKeyFrames>
  81. <PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/>
  82. </Storyboard>
  83. </VisualState>
  84. <VisualState x:Name="Disabled">
  85. <Storyboard>
  86. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
  87. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
  88. </ObjectAnimationUsingKeyFrames>
  89. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
  90. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}"/>
  91. </ObjectAnimationUsingKeyFrames>
  92. <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
  93. <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
  94. </ObjectAnimationUsingKeyFrames>
  95. </Storyboard>
  96. </VisualState>
  97. </VisualStateGroup>
  98. </VisualStateManager.VisualStateGroups>
  99. <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
  100. </Grid>
  101. </ControlTemplate>
  102. </Setter.Value>
  103. </Setter>
  104. </Style>
  105. </Page.Resources>
  106.  
  107. </Page>

OK
[源码下载]

背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI的更多相关文章

  1. 控件 UI: VisualState, VisualStateManager, 控件的默认 UI

    VisualState 和 VisualStateManager 控件的默认 Style, ControlTemplate, VisualState 示例1.演示“VisualState 和 Visu ...

  2. 背水一战 Windows 10 (8) - 控件 UI: StateTrigger

    [源码下载] 背水一战 Windows 10 (8) - 控件 UI: StateTrigger 作者:webabcd 介绍背水一战 Windows 10 之 控件 UI VisualState 之 ...

  3. 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate

    [源码下载] 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate 作者:webabcd 介绍背水一战 Windows 10 ...

  4. 背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理

    [源码下载] 背水一战 Windows 10 (79) - 自定义控件: Layout 系统, 控件模板, 事件处理 作者:webabcd 介绍背水一战 Windows 10 之 控件(自定义控件) ...

  5. 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素

    [源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...

  6. 背水一战 Windows 10 (48) - 控件(集合类): FlipView

    [源码下载] 背水一战 Windows 10 (48) - 控件(集合类): FlipView 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) FlipView 示例Fl ...

  7. 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter

    [源码下载] 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter 作者:weba ...

  8. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  9. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

随机推荐

  1. 12小时包你学会基于ReactMix框架的ReactNativeApp开发(一)Hello World!

    ReactMixhttps://github.com/xueduany/react-mix自从昨天发布起来,得到了不少小伙伴的热捧,很高兴帮助大家解决了憋在心中很久的问题“如果我只会HTML,Css, ...

  2. linux网络编程系列-网络连接的建立

    一个比较实用的连接函数,支持host为域名. #include <netdb.h> #include <sys/socket.h> #include <sys/types ...

  3. Android 数据传递(一) Activity之间的数据传递

    bundle Google Bundle类说明 Bundle类是一个key-value对.Activity之间的数据通信可以通过bundle类来实现数据的存储.即将数据放入bundle里面,将Bund ...

  4. IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop

    注:本人是翻译过来,并且加上本人的一点见解. 要点: 1.前言 2.pthread 3.NSThread 4.Grand Central Dispatch(GCD) 5.Operation Queue ...

  5. 好脑袋不如烂笔头-Quartz使用总结

    Quartz是Java平台的一个开源的作业调度框架.Quartz.net是从java版本移植到.net版本的..net项目使用Quartz来执行批处理等定时任务非常方便. (1)从nuget上可以安装 ...

  6. [开发工具]Java开发常用的在线工具

    注明: 本文转自http://www.hollischuang.com/archives/1459.作为一个Java开发人员,经常要和各种各样的工具打交道,除了我们常用的IDE工具以外,其实还有很多工 ...

  7. atitit.TokenService v3 qb1  token服务模块的设计 新特性.docx

    atitit.TokenService v3 qb1  token服务模块的设计 新特性.docx 1.1. V3 新特性1 1.2. V2 新特性1 2. Token的归类1 3. Token的用途 ...

  8. paip.禁用IKAnalyzer 的默认词库.仅仅使用自定义词库.

    paip.禁用IKAnalyzer 的默认词库.仅仅使用自定义词库. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http:// ...

  9. andriod sdk模拟器安装过程中报错

    andriod sdk模拟器安装过程中,出现下述错误: Failed to fetch URL http://dl-ssl.google.com/android/repository/reposito ...

  10. 注意HTML的语言编码charset

    注意HTML的语言编码的重要性 目录 charset编码重要性 charset在html什么地方 charset标签 编码种类 charset utf-8介绍 charset GB2312介绍 推荐网 ...