背水一戰 Windows 10 (7) - 控制項 UI: VisualState, VisualStateManager, 控制項的預設 UI

来源:http://www.cnblogs.com/webabcd/archive/2016/04/18/5404196.html
-Advertisement-
Play Games

[源碼下載] 背水一戰 Windows 10 (7) - 控制項 UI: VisualState, VisualStateManager, 控制項的預設 UI 作者:webabcd介紹背水一戰 Windows 10 之 控制項 UI VisualState 和 VisualStateManager 控制項的 ...


[源碼下載]


背水一戰 Windows 10 (7) - 控制項 UI: VisualState, VisualStateManager, 控制項的預設 UI



作者:webabcd


介紹
背水一戰 Windows 10 之 控制項 UI

  • VisualState 和 VisualStateManager
  • 控制項的預設 Style, ControlTemplate, VisualState



示例
1、演示“VisualState 和 VisualStateManager”相關知識點
Controls/UI/VisualState/VisualStateDemo.xaml

<Page
    x:Class="Windows10.Controls.UI.VisualState.VisualStateDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.UI.VisualState"
    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 中定義 VisualState 和 VisualStateManager
            -->
            <ControlTemplate x:Key="ControlTemplate1" 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 - 滑鼠經過時的狀態(詳細的過渡效果在後面的 VisualStateGroup.Transitions 中定義)
                            -->
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <ColorAnimation 
                                        Storyboard.TargetName="borderBrush" 
                                        Storyboard.TargetProperty="Color" 
                                        To="Green" />
                                </Storyboard>
                            </VisualState>
                            
                            <!--
                                Pressed - 滑鼠按下時的狀態
                            -->
                            <VisualState x:Name="Pressed">
                                <VisualState.Storyboard>
                                    <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.Storyboard>
                                <VisualState.Setters>
                                    <!--
                                        這部分是 uwp 新增的特性,以前只能通過 Storyboard 來實現
                                    -->
                                    <Setter Target="grid.Width" Value="100" />
                                </VisualState.Setters>
                                <VisualState.StateTriggers>
                                    <!--
                                        這部分是 uwp 新增的特性
                                        關於 StateTriggers 請參見 /Controls/UI/VisualState/StateTrigger.xaml
                                    -->
                                </VisualState.StateTriggers>
                            </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="MyStates">
                            <VisualState x:Name="MyState1" />
                            <VisualState x:Name="MyState2"/>
                            <VisualState x:Name="MyState3"/>
                        </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="10 0 10 10">

            <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5" />

            <Button Name="btnDemo" Content="我是 Button(用於演示 VisualState 和 VisualStateManager)" Margin="5" Background="Blue" Foreground="White" Template="{StaticResource ControlTemplate1}" />

            <Button Name="btnVisualStateManager" Content="將上面的按鈕的 VisualState 轉到 PointerOver" Click="btnVisualStateManager_Click" Margin="5" />

        </StackPanel>

    </Grid>
</Page>

Controls/UI/VisualState/VisualStateDemo.xaml.cs

/*
 * 演示“VisualState 和 VisualStateManager”相關知識點
 */

using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows10.Common;

namespace Windows10.Controls.UI.VisualState
{
    public sealed partial class VisualStateDemo : Page
    {
        public VisualStateDemo()
        {
            this.InitializeComponent();
        }

        private void btnVisualStateManager_Click(object sender, RoutedEventArgs e)
        {
            /*
             * bool GoToState(Control control, string stateName, bool useTransitions) - 轉換 VisualState
             *     control - 需要轉換 VisualState 的控制項
             *     stateName - 目標 VisualState 的名稱
             *     useTransitions - 是否使用 VisualTransition 進行過渡
             */

            // 將 VisualState 轉到指定的狀態(每個 VisualStateGroup 分別指定一個其內的 VisualState)
            VisualStateManager.GoToState(btnDemo, "PointerOver", true);
            VisualStateManager.GoToState(btnDemo, "MyState3", false);



            /*
             * VisualStateManager.GetVisualStateGroups(FrameworkElement obj) - 獲取指定 FrameworkElement 中的 VisualStateGroup 集合
             *     註:本例中的 VisualState 定義在 btnDemo 的控制項模板中的第一個 Grid 中
             *
             * VisualStateGroup - VisualState 組(每個 VisualStateManager 下可以有多個 VisualStateGroup)
             *     Name - 獲取此 VisualStateGroup 的名字
             *     CurrentState - 獲取此 VisualStateGroup 的當前使用的 VisualState(每個 VisualStateGroup 正在使用的 VisualState 只能有一個)
             *     States - 獲取此 VisualStateGroup 中的 VisualState 集合
             *     Transitions - 獲取此 VisualStateGroup 中的 VisualTransition 集合
             *     CurrentStateChanging, CurrentStateChanged - 此 VisualStateGroup 中的正在使用的 VisualState 發生變化時觸發的事件
             *
             * VisualState - VisualState
             *     Name - 獲取此 VisualState 的名字
             *     Setters - 獲取此 VisualState 中的 Setter 集合
             *     StateTriggers - 獲取此 VisualState 中的 StateTrigger 集合
             *     Storyboard - 獲取此 VisualState 中的 Storyboard 對象
             */

            lblMsg.Text = "";
            Grid grid = Helper.GetVisualChild<Grid>(btnDemo);
            IList<VisualStateGroup> visualStateGroups = VisualStateManager.GetVisualStateGroups(grid);
            foreach (VisualStateGroup visualStateGroup in visualStateGroups)
            {
                lblMsg.Text += visualStateGroup.Name + " " + visualStateGroup.CurrentState.Name;
                lblMsg.Text += Environment.NewLine;
            }
        }
    }
}


2、演示如何獲取控制項的預設 Style, ControlTemplate, VisualState
Controls/UI/DefaultUI.xaml

<Page
    x:Class="Windows10.Controls.UI.DefaultUI"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.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="10 0 10 10">

            <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="0 10 0 0">
                
                <Run>如何獲取控制項的預設 Style, ControlTemplate, VisualState 呢?</Run>
                <LineBreak />
                <Run>1、在 msdn 上找: https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/mt299122.aspx</Run>
                <LineBreak />
                <Run>2、在 Visual Studio 的設計界面,右鍵選擇控制項,然後選擇“編輯控制項”或“編輯模板”或“編輯其他模板”,然後選擇“編輯副本”</Run>

            </TextBlock>

        </StackPanel>
    </Grid>

    <Page.Resources>
        <!--
            這個就是 Button 的預設樣式
        -->
        <Style x:Key="ButtonStyle1" TargetType="Button">
            <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
            <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
            <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
            <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
            <Setter Property="Padding" Value="8,4,8,4"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
            <Setter Property="UseSystemFocusVisuals" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal">
                                        <Storyboard>
                                            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <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}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>

</Page>



OK
[源碼下載]


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 當你完成組件註冊,並將組件暴露為適當的服務後你就可以通過容器或者容器的子生命周期域來解析服務(After you have your components registered with appropriate services exposed, you can resolve services f ...
  • 原帖地址 另外一個 去掉編輯器的下邊欄 在config.js中加入: config.removePlugins = 'elementspath'; config.resize_enabled = false; 就ok了 ckeditor+ckfinder配置用法 一、使用方法: 1、在頁面<head ...
  • 入住博客園4年多了,一直都是看別人的博客,學習別人的知識,為各個默默無私貢獻自己技術總結的朋友們頂一個;這幾天突然覺得是時候加入該隊列中,貢獻出自己微弱的力量,努力做到每個月有不同學習總結,知識學習的分享文章。以下要分享的是花了兩天時間編寫+測試的windows下C#定時管理器框架-Task.Mai ...
  • ...
  • TabControl控制項的TabItem的Content元素,例如:DataGrid控制項,在對事件的處理時,需要對事件的源引起關註,當需要處理DataGrid的事件時,事件會傳遞到TabControl中,解決這種問題的方法如下: 方法一:判斷觸發此事件的源是誰,根據事件的源再做相應處理 方法二:註冊 ...
  • wpf
    1.WPF 設置TextBox為空時,背景為文字提示。 <TextBox FontSize="17" Height="26" Margin="230,150,189,0" Name="txt_Account" VerticalAlignment="Top" Foreground="Indigo" T ...
  • 彈幕功能通常用於實時顯示當前視頻或者文檔的評論內容,在上快速飛過的方式呈現,看起來比較酷炫。 這種典型的多用戶實時交互的功能,很適合使用SignalR實現,通過SignalR提供後臺的服務推送功能,客戶端接收消息後呈現出來。 彈幕功能實現起來有點類似聊天室的功能,只是消息的展示方式不同,所以結合Si ...
  • 什麼是LocalDB 隨著SQL Server 2012的發佈,LocalDB躍入我們的視線,它可以被看做是SQL Server Express的輕量級版本。LocalDB專門為開發人員創建,它非常易於安裝,幾乎無需管理,相容T-SQL語言,編程介面與SQL Server Express別無二致。有 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...