背水一戰 Windows 10 (10) - 資源: StaticResource, ThemeResource

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

[源碼下載] 背水一戰 Windows 10 (10) - 資源: StaticResource, ThemeResource 作者:webabcd介紹背水一戰 Windows 10 之 資源 StaticResource ThemeResource 示例1、演示“StaticResource”相關 ...


[源碼下載]


背水一戰 Windows 10 (10) - 資源: StaticResource, ThemeResource



作者:webabcd


介紹
背水一戰 Windows 10 之 資源

  • StaticResource
  • ThemeResource



示例
1、演示“StaticResource”相關知識點
Resource/StaticResourceDemo.xaml

<Page
    x:Class="Windows10.Resource.StaticResourceDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Resource"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    
    xmlns:common="using:Windows10.Common">

    <!--
        定義各種類型的 StaticResource
        系統會在載入 xaml 的時候去查找並應用 StaticResource 指定的資源(註:和 StaticResource 相對應的 DynamicResource 在 uwp 中不支持)
    -->
    <Page.Resources>
        <x:String x:Key="MyString">StaticResource 經常用於指定 Style 或 ControlTemplate 資源,參見 /Controls/UI 部分</x:String>
        <x:Double x:Key="MyDouble1">24</x:Double>
        <x:Double x:Key="MyDouble2">48</x:Double>
        <Thickness x:Key="MyThickness">20,20,20,20</Thickness>
        <common:Employee x:Key="CurrentEmployee" Name="wanglei" Age="35" IsMale="True"></common:Employee>
    </Page.Resources>
    
    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">
            
            <!--
                下麵演示 StaticResource 的使用方法
            -->
            
            <TextBlock Name="textBlock1" Margin="5" Text="{StaticResource MyString}" />

            <TextBlock Name="textBlock2" Margin="5" Text="{StaticResource ResourceKey=MyString}" />

            <TextBlock Name="textBlock3" Margin="5" FontSize="{StaticResource MyDouble1}" Text="我是 TextBlock">
                <!--
                    Style 或 ControlTemplate 內都可以引用靜態資源
                -->
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Padding" Value="{StaticResource MyThickness}" />
                    </Style>
                </TextBlock.Style>
            </TextBlock>

            <!--動態改變引用的資源-->
            <Button Name="btnChangeStaticResource" Content="改變引用的 StaticResource" Margin="5" Click="btnChangeStaticResource_Click" />

            <!--
                設置 FrameworkElement 的 DataContext 為一個指定的靜態資源
            -->
            <TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding Name}" />
            <TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding Age}" />
            <TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding IsMale}" />
            
        </StackPanel>
    </Grid>
</Page>

Resource/StaticResourceDemo.xaml.cs

/*
 * 演示“StaticResource”相關知識點
 */

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.Resource
{
    public sealed partial class StaticResourceDemo : Page
    {
        public StaticResourceDemo()
        {
            this.InitializeComponent();
        }

        private void btnChangeStaticResource_Click(object sender, RoutedEventArgs e)
        {
            // 獲取 Application 中的資源
            // (double)Application.Current.Resources["MyDouble1"];

            // 獲取關聯 xaml 內的資源(本例中的資源定義在 xaml 中的 Page 下,所以用 this.Resources[""] 來獲取)
            if (textBlock3.FontSize == (double)this.Resources["MyDouble1"])
            {
                // 引用指定的資源
                textBlock3.FontSize = (double)this.Resources["MyDouble2"];
            }
            else
            {
                textBlock3.FontSize = (double)this.Resources["MyDouble1"];
            }
        }
    }
}


2、演示“ThemeResource”相關知識點
Resource/ThemeResourceDemo.xaml

<Page
    x:Class="Windows10.Resource.ThemeResourceDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Resource"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <!--
        ThemeResource 與 StaticResource 的區別是:ThemeResource 在運行時會根據主題的變化而重新計算
    -->
    
    <!--
        預設的主題資源的相關定義在如下位置(以我的開發環境為例)
        1、C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10586.0\Generic\generic.xaml
        2、C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10586.0\Generic\themeresources.xaml
        
        註:預設的主題資源不會複製到應用程式中,這些資源字典在記憶體中作為 Windows 運行時本身的一部分存在
    -->

    <Page.Resources>
        
        <ResourceDictionary>
            <!--
                通過 ResourceDictionary 內的 ResourceDictionary.ThemeDictionaries 內的 ResourceDictionary 來定義不同主題的資源
                在資源中定義的主題分為 3 種:"Light", "Dark" 和 "HighContrast",其中 High Contrast(高對比度模式) 不常用,就不詳細介紹了
            -->
            <ResourceDictionary.ThemeDictionaries>
                
                <!--
                    Default 主題,對應 ElementTheme.Dark 或 ApplicationTheme.Dark
                -->
                <ResourceDictionary x:Key="Default">
                    <!--
                        這裡摘自 themeresources.xaml 中的部分定義,如果要覆蓋其中的定義就自己再定義同名資源即可
                    -->
                    <SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="#FF000000" />
                    <SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemAccentColor}" />
                    
                    <!--
                        這是系統級資源,不在 themeresources.xaml 內,其含義是在“設置”->“個性化”->“顏色”中選擇的主題色,當然也可以這樣重新定義
                    -->
                    <Color x:Key="SystemAccentColor">#FFFF0000</Color>
                </ResourceDictionary>

                <!--
                    HighContrast 主題,不常用,就不詳細介紹了
                -->
                <ResourceDictionary x:Key="HighContrast">
                    <!--
                        這裡摘自 themeresources.xaml 中的部分定義,其引用的一些顏色資源來自系統級,比如 SystemColorWindowColor 或 SystemColorButtonFaceColor 之類的,他們不在 themeresources.xaml 內
                        比如在“設置”->“輕鬆使用”->“高對比度”中目前可以設置 4 中不同的高對比度主題,每一種對應的顏色資源都不一樣
                    -->
                    <SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="{ThemeResource SystemColorWindowColor}" />
                    <SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemColorButtonFaceColor}" />
                </ResourceDictionary>

                <!--
                    Light 主題,對應 ElementTheme.Light 或 ApplicationTheme.Light
                -->
                <ResourceDictionary x:Key="Light">
                    <!--
                        這裡摘自 themeresources.xaml 中的部分定義,如果要覆蓋其中的定義就自己再定義同名資源即可
                    -->
                    <SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="#FFFFFFFF" />
                    <SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemAccentColor}" />

                    <!--
                        這是系統級資源,不在 themeresources.xaml 內,其含義是在“設置”->“個性化”->“顏色”中選擇的主題色,當然也可以這樣重新定義
                    -->
                    <Color x:Key="SystemAccentColor">#FF00FF00</Color>
                </ResourceDictionary>

            </ResourceDictionary.ThemeDictionaries>
        </ResourceDictionary>

    </Page.Resources>
    
    <Grid Background="Transparent">
        
        <StackPanel Name="panel" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10 0 10 10">

            <TextBlock Margin="5" Name="lblMsg" Foreground="Blue"  />

            <TextBlock Margin="5" Text="Microsoft HoloLens全息眼鏡由Microsoft 公司於北京時間2015年1月22日凌晨與Window10同時發佈。" Foreground="Blue" />

            <StackPanel Width="200" Height="100" Margin="5" HorizontalAlignment="Left" Background="{ThemeResource SystemControlBackgroundAccentBrush}" />

            <!--動態變換主題,引用的主題資源會重新計算-->
            <Button Name="btnChangeTheme" Click="btnChangeTheme_Click" Margin="5">變換主題</Button>

        </StackPanel>
        
    </Grid>
</Page>

Resource/ThemeResourceDemo.xaml.cs

/*
 * 演示“ThemeResource”相關知識點
 *
 *
 * 1、主題共有兩種類別:Light 和 Dark,子會繼承父的主題類別
 * 2、Application 級別指定 Theme 的話,在 App.xaml 中做如下聲明 <Application RequestedTheme="Dark"></Application>
 * 3、FrameworkElement 級別指定 Theme 的話,則指定 FrameworkElement.RequestedTheme 即可
 *
 *
 * Application.Current.RequestedTheme - 獲取或設置 Application 級別的主題(ApplicationTheme 枚舉:Light, Dark)
 * FrameworkElement.RequestedTheme - 獲取或設置 FrameworkElement 級別的主題(ElementTheme 枚舉:Default, Light, Dark)
 * 註:ElementTheme 比 ApplicationTheme 多了一個 Default,其含義是當 ElementTheme 為 Default 時,其實際主題為 application 級主題
 */

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Windows10.Resource
{
    public sealed partial class ThemeResourceDemo : Page
    {
        public ThemeResourceDemo()
        {
            this.InitializeComponent();

            DisplayMessage();
        }

        private void DisplayMessage()
        {
            // 當前 Application 級別的 Theme
            lblMsg.Text = "application theme: " + Application.Current.RequestedTheme.ToString();
            lblMsg.Text += Environment.NewLine;

            // 當前 panel 的 Theme
            lblMsg.Text += "FrameworkElement  theme: " + panel.RequestedTheme.ToString();
        }

        // 動態變換主題,引用的主題資源會重新計算
        private void btnChangeTheme_Click(object sender, RoutedEventArgs e)
        {
            if (panel.RequestedTheme == ElementTheme.Default)  // 未指定 panel 的主題,則 panel 主題同 application 級主題
            {
                if (Application.Current.RequestedTheme == ApplicationTheme.Dark) // application 是 Dark 主題
                {
                    panel.RequestedTheme = ElementTheme.Light;
                }
                else
                {
                    panel.RequestedTheme = ElementTheme.Dark;
                }
            }
            else if (panel.RequestedTheme == ElementTheme.Dark) // panel 是 Dark 主題
            {
                panel.RequestedTheme = ElementTheme.Light;
            }
            else // panel 是 Light 主題
            {
                panel.RequestedTheme = ElementTheme.Dark;
            }

            DisplayMessage();
        }
    }
}

App.xaml

<Application
    x:Class="Windows10.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10"
    
    RequestedTheme="Dark">
    <!--
        上面的 RequestedTheme 被我設置為 Dark 了,關於主題的相關知識點請參見:/Resource/ThemeResourceDemo.xaml
    -->

</Application>

 


OK
[源碼下載]


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

-Advertisement-
Play Games
更多相關文章
  • 無聊練習一下WinForm,輸入網址,點擊按鈕就在瀏覽器打開網址。 源代碼下載: http://hovertree.com/h/bjaf/cao15h74.htm ...
  • 平臺之大勢何人能擋? 帶著你的Net飛奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Article/Detail/330 先建一個用戶 FTp角色添加一下 打開IIS,添加FTP站(如果發 ...
  • 第一次自己寫博客文章,大家多多指教。寫博客主要記錄一下學習的過程,給初學者提供下參考,也留給自己做備忘。 Slickflow .NET開源工作流-環境搭建 在VS2010中使用附加進程的方式調試IIS中的頁面,請參考如下頁面: http://www.cnblogs.com/minesky/p/338 ...
  • 任務描述:通過科大訊飛語音合成組件線上完成文本轉語音的合成,然後再轉換為電話系統IVR要求的音頻格式: wave mu-law 16位 8kHZ 64kbps。 完成步驟: 首先,我們要先通過科大訊飛語音合成組件實現文本合成,由於科大訊飛提供的介面都是C語言的,如果用C#調用需要做二次封裝,為了快速 ...
  • .Net 動態代理,AOP 直接上代碼了。 DEMO: 也可以到我的Github上,直接獲取完整項目 https://github.com/jinshuai/DynamicProxy.NET ...
  • 官方開髮指導https://autopoco.codeplex.com/documentation 初步使用: SimpleUser是自己要批量創建的類 1)創建管理工廠 IGenerationSessionFactory factory = AutoPocoContainer.Configure( ...
  • 是一般處理程式, 是asp.net web 組件的一種,ashx是其擴展名。 實現IHttpHandler介面,接收並處理http請求。這個介面有一個IsReusable成員,一個待實現的方法ProcessRequest(HttpContextctx) 。程式在processRequest方法中處理 ...
  • 1.【開源】C#跨平臺物聯網通訊框架ServerSuperIO(SSIO) 2.應用SuperIO(SIO)和開源跨平臺物聯網框架ServerSuperIO(SSIO)構建系統的整體方案 3.C#工業物聯網和集成系統解決方案的技術路線(數據源、數據採集、數據上傳與接收、ActiveMQ、Mongod ...
一周排行
    -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# ...