Extensions in UWP Community Toolkit - ViewExtensions

来源:https://www.cnblogs.com/shaomeng/archive/2018/04/12/8744621.html
-Advertisement-
Play Games

概述 UWP Community Toolkit Extensions 中有一個為 View 提供的擴展 - View Extensions,本篇我們結合代碼詳細講解 View Extensions 的實現。 View Extensions 包括了 ApplicationViewExtensions ...


概述

UWP Community Toolkit Extensions 中有一個為 View 提供的擴展 - View Extensions,本篇我們結合代碼詳細講解 View Extensions 的實現。

View Extensions 包括了 ApplicationViewExtensions,StatusBarExtensions 和 TitleBarExtensions,讓開發者可以方便的定製 AppView,StatusBar 和 TitleBar 的樣式,接下來看看官方示例的截圖:

Source: https://github.com/Microsoft/UWPCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Extensions/ApplicationView

             https://github.com/Microsoft/UWPCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Extensions/StatusBar

             https://github.com/Microsoft/UWPCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Extensions/TitleBar

Doc: https://docs.microsoft.com/zh-cn/windows/uwpcommunitytoolkit/extensions/viewextensions

Namespace: Microsoft.Toolkit.Uwp.UI.Extensions; Nuget: Microsoft.Toolkit.Uwp.UI;

 

開發過程

代碼分析

由於 ViewExtensions 分為 ApplicationViewExtensions,StatusBarExtensions 和 TitleBarExtensions 三個部分,我們分別來看一下:

1. ApplicationViewExtensions

先來看一下 ApplicationViewExtensions 的結構:

 

雖然有兩個類組成,但其實 ApplicationView.cs 類是 Obsolete 的,所以現在在使用的是 ApplicationViewExtensions.cs,我們主要看一下這個類,先看一下類結構:

類的功能比較簡單,我們主要來看這幾個針對 Page 的附加屬性對應的 get 和 set 方法:

  • Title 對應 GetTitle(page) 和 SetTitle(page, value) - 獲取和設置 App 標題,主要處理邏輯是通過 GetApplicationView() 獲取 applicationView,然後再獲取或設置 Title 屬性;
  • ExtendViewIntoTitleBar 對應 GetExtendViewIntoTitleBar(page) 和 SetExtendViewIntoTitleBar(page, value) - 獲取和設置是否擴展視圖到標題欄的布爾值,主要處理邏輯是通過 GetCoreApplicationView() 獲取 CoreApplicationView,然後再獲取或設置這個屬性,如果為 True,那麼 App 的 UI 會占據 TitleBar 的位置;
  • BackButtonVisibility 對應 GetBackButtonVisibility(page) 和 SetBackButtonVisibility(page, value) - 獲取和設置後退按鈕是否可用,主要處理邏輯是通過 GetSystemNavigationManager() 來獲取 SystemNavigationManager,然後再設置或獲取這個屬性;

2. StatusBarExtensions 

先來看一下 StatusBarExtensions 的結構:

和 ApplicationViewExtensions 類似,StatusBar.cs 類是 Obsolete 的,所以現在在使用的是 StatusBarExtensions.cs,我們主要看一下這個類,先看一下類結構:

 

類的功能比較簡單,我們主要來看這幾個針對 Page 的附加屬性對應的 get 和 set 方法:

  • BackgroundColor 對應 GetBackgroundColor(page) 和 SetBackgroundColor(page, color) - 獲取和設置 StatusBar 的背景顏色,主要通過 GetStatusBar() 獲得 StatusBar 實例,然後獲取或設置 BackgroundColor 屬性;
  • ForegroundColor 對應 GetForegroundColor(page) 和 SetForegroundColor(page, color) - 獲取和設置 StatusBar 的前景顏色,主要通過 GetStatusBar() 獲得 StatusBar 實例,然後獲取或設置 ForegroundColor 屬性;
  • BackgroundOpaticy 對應 GetBackgroundOpaticy(page) 和 SetBackgroundOpaticy(page, color) - 獲取和設置 StatusBar 的背景透明度,主要通過 GetStatusBar() 獲得 StatusBar 實例,然後獲取或設置 BackgroundOpaticy 屬性;
  • IsVisible 對應 GetIsVisible(page) 和 SetIsVisible(page, double) - 獲取和設置 StatusBar 是否可見,獲取方法通過獲取 OccludedRect Height 的高度來判斷是否可見,因為 InputPane 的 VIsible 屬性只在 XBox 有效;設置是通過 Page 的 IsVisibleProperty 屬性來設置;IsVisibleProperty 是類中定義的依賴屬性,改變時觸發 OnIsVisibleChanged 事件;

來看一下 OnIsVisibleChanged 事件的處理方法,通過調用 StatusBar 的 ShowAsync() 和 HideAsync() 方法來設置 StatusBar 的可見和不可見;

private static async void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var statusBar = GetStatusBar();

    if (statusBar == null)
    {
        return;
    }

    bool isVisible = (bool)e.NewValue;

    if (isVisible)
    {
        await statusBar.ShowAsync();
    }
    else
    {
        await statusBar.HideAsync();
    }
}

3. TitleBarExtensions 

先來看一下 TitleBarExtensions 的結構: 

和 ApplicationViewExtensions 類似,TitleBar.cs 類是 Obsolete 的,所以現在在使用的是 TitleBarExtensions.cs,我們主要看一下這個類,先看一下類結構: 

類的功能比較簡單,我們主要來看這幾個針對 Page 的附加屬性對應的 get 和 set 方法:

  • BackgroundColor 對應 GetBackgroundColor(page) 和 SetBackgroundColor(page, color)  - 獲取和設置 TitleBar 的背景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 BackgroundColor 屬性;在顯示上會覆蓋 StatusBar 的對應屬性;
  • ButtonBackgroundColor 對應 GetButtonBackgroundColor(page) 和 SetButtonBackgroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕的背景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonBackgroundColor 屬性;
  • ButtonForegroundColor 對應 GetButtonForegroundColor(page) 和 SetButtonForegroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕的前景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonForegroundColor 屬性;
  • ButtonHoverBackgroundColor 對應 GetButtonHoverBackgroundColor(page) 和 SetButtonHoverBackgroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕的滑鼠懸浮背景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonHoverBackgroundColor 屬性;
  • ButtonHoverForegroundColor 對應 GetButtonHoverForegroundColor(page) 和 SetButtonHoverForegroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕的滑鼠懸浮前景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonHoverForegroundColor 屬性;
  • ButtonInactiveBackgroundColor 對應 GetButtonInactiveBackgroundColor(page) 和 SetButtonInactiveBackgroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕在視窗非活動狀態時的背景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonInactiveBackgroundColor 屬性;
  • ButtonInactiveForegroundColor 對應 GetButtonInactiveForegroundColor(page) 和 SetButtonInactiveForegroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕在視窗非活動狀態時的前景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonInactiveForegroundColor 屬性;
  • ButtonPressedBackgroundColor 對應 GetButtonPressedBackgroundColor(page) 和 SetButtonPressedBackgroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕點擊時的背景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonPressedBackgroundColor 屬性;
  • ButtonPressedForegroundColor 對應 GetButtonPressedForegroundColor(page) 和 SetButtonPressedForegroundColor(page, color) - 獲取和設置 TitleBar 的右上角三個按鈕點擊時的前景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ButtonPressedForegroundColor 屬性;
  • ForegroundColor 對應 GetForegroundColor(page) 和 SetForegroundColor(page, color)  - 獲取和設置 TitleBar 的前景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 ForegroundColor 屬性;在顯示上會覆蓋 StatusBar 的對應屬性;
  • InactiveBackgroundColor 對應 GetInactiveBackgroundColor(page) 和 SetInactiveBackgroundColor(page, color)  - 獲取和設置 TitleBar 在視窗非活動時的背景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 InactiveBackgroundColor 屬性;在顯示上會覆蓋 StatusBar 的對應屬性;
  • InactiveForegroundColor 對應 GetInactiveForegroundColor(page) 和 SetInactiveForegroundColor(page, color)  - 獲取和設置 TitleBar 在視窗非活動時的前景色,主要通過 GetTitleBar() 方法獲得 TitleBar 實例,然後獲取或設置 InactiveForegroundColor 屬性;在顯示上會覆蓋 StatusBar 的對應屬性;

 

調用示例

我們定製了 AppView 的 Title,StatusBar 和 TitleBar 的樣式,看到運行圖和設置的一致;

<Page
    x:Class="CommunityToolkitSample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:extensions="using:Microsoft.Toolkit.Uwp.UI.Extensions"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    extensions:ApplicationViewExtensions.Title="View Extensions"
      extensions:TitleBarExtensions.BackgroundColor="Red"
      extensions:TitleBarExtensions.ForegroundColor="Green"
      extensions:TitleBarExtensions.ButtonBackgroundColor="Gray"
      extensions:TitleBarExtensions.ButtonForegroundColor="White"
      extensions:StatusBarExtensions.BackgroundColor="CornflowerBlue"
      extensions:StatusBarExtensions.BackgroundOpacity="0.8"
      extensions:StatusBarExtensions.ForegroundColor="White"
      extensions:StatusBarExtensions.IsVisible="False"
    mc:Ignorable="d">

 

總結

到這裡我們就把 UWP Community Toolkit Extensions 中的 View Extensions 的源代碼實現過程和簡單的調用示例講解完成了,希望能對大家更好的理解和使用這個擴展有所幫助。歡迎大家多多交流,謝謝!

最後,再跟大家安利一下 UWPCommunityToolkit 的官方微博:https://weibo.com/u/6506046490大家可以通過微博關註最新動態。

衷心感謝 UWPCommunityToolkit 的作者們傑出的工作,Thank you so much, UWPCommunityToolkit authors!!!

 


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

-Advertisement-
Play Games
更多相關文章
  • 一、文件的打開和關閉 1.常用的打開關閉語句 第一行是打開名為“yesterday”的文件,只讀屬性,編碼方式為utf-8。如果沒有指定文件打開方式,預設只讀“r”,如果沒有指定編碼方式可能會部分亂碼。 第二行是關閉文件 2.with as語句 目的:為了防止程式員打開文件後忘記關閉文件 3.文件打 ...
  • 要為掃雷游戲佈置地雷,掃雷游戲的掃雷面板可以用二維int數組表示。如某位置為地雷,則該位置用數字-1表示, 如該位置不是地雷,則暫時用數字0表示。 編寫程式完成在該二維數組中隨機佈雷的操作,程式讀入3個參數:佈雷面板的行數(r),列數(c),佈置的地雷個數(n), 且要滿足0<n<r*c*0.75( ...
  • import org.apache.http.HttpEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apac ...
  • 實現過程 A 創建三個服務 一主二從模式 B 實現一主二從關係 C 創建sentinel.conf文件 D 增加以下內容 1.sentinel monitor <master-name> <ip> <redis-port> <quorum> 告訴sentinel去監聽地址為ip:port的一個mas ...
  • 1.Slice(切片)代表變長的序列,序列中每個元素都有相同的類型,一個slice類型一般寫作[]T,其中T代表slice中元素的類型;slice的語法和數組很像,只是沒有固定長度而已,slice的底層確實引用一個數組對象 2.內置的len和cap函數分別返回slice的長度和容量 3.s[i:j] ...
  • Java 內部類分為: 1)成員內部類 2)靜態嵌套類 3)方法內部類 4)匿名內部類 內部類的共性 1、內部類仍然是一個獨立的類,在編譯之後內部類會被編譯成獨立的.class文件,但是前面冠以外部類的類名和$符號 。 2、內部類不能用普通的方式訪問。內部類是外部類的一個成員,因此內部類可以自由地訪 ...
  • 方法 綁定方法和非綁定方法 綁定方法和非綁定方法在創建時沒有任何區別,同一方法,既可以為綁定方法,也可以為非綁定方法,一切不同都只在調用時的手法上有所區別。 綁定方法即該方法綁定類的一個實例上,必須將self作為第一個參數傳入,而這個過程是由Python自動完成。可以通過實例名.方法名(參數列表)來 ...
  • 出現這個問題的原因可能很多,但是最終原因都是部署的項目文件中沒有這個類包。 那麼出錯的點在哪呢?逐一排除! 1.首先在項目文件中沒有添加相應的jar包,可以在maven dependencis文件夾中看是否有。如果沒有,在pom文件添加依賴配置即可;如果有,還是出現問題轉第二步 2.在maven的本 ...
一周排行
    -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# ...