概述git
UWP Community Toolkit 中有一個爲 Frmework Element 提供投影效果的控件 - DropShadowPanel,本篇咱們結合代碼詳細講解 DropShadowPanel 的實現。github
DropShadowPanel 提供的陰影效果有不少應用場景,好比給文本提供陰影,能夠讓文本在背景變化時能夠明顯顯示,好比地圖上的標尺文本;應用在圖形或圖片時,能夠設置陰影效果,另外它有不少參數能夠調整,如陰影偏移,顏色,透明度和陰影模糊半徑等,14393 開始支持,下面看看官方示例的截圖:windows
Doc: https://docs.microsoft.com/zh-cn/windows/uwpcommunitytoolkit/controls/dropshadowpanelthis
Namespace: Microsoft.Toolkit.Uwp.UI.Controls; Nuget: Microsoft.Toolkit.Uwp.UI.Controls;spa
開發過程code
代碼分析blog
咱們先來看看 DropShadowPanel 控件的類構成:繼承
1. DropShadowPanel.Properties.cs事件
DropShadowPanel 控件部分類的依賴屬性類,定義瞭如下依賴屬性:
2. DropShadowPanel.cs
DropShadowPanel 控件的定義和處理邏輯,類繼承自 ContentControl;先來看看構造方法:
肯定系統版本支持知足條件後,建立一個 Compositor 和 dropShadow,建立 shadowVisual,並用 dropShadow 給它賦值;
public DropShadowPanel() { this.DefaultStyleKey = typeof(DropShadowPanel); if (IsSupported) { Compositor compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _shadowVisual = compositor.CreateSpriteVisual(); _dropShadow = compositor.CreateDropShadow(); _shadowVisual.Shadow = _dropShadow; } }
接着來看一下上面 DropShadowPanel.Properties.cs 中屬性的變化觸發事件,實際的處理方法都在 DropShadowPanel.cs 類中,處理過程就是把 newValue 賦值給 dropShadow;
而在 OnSizeChanged(s, e) 事件觸發時,以及初始化時,主要處理邏輯是 UpdateShadowSize() 方法:
方法處理中,獲取須要設置投影的 Content,獲得實際顯示寬度和高度,賦值給 shadowVisual;做爲 dropShadow 的顯示宿主;
private void UpdateShadowSize() { if (_shadowVisual != null) { Vector2 newSize = new Vector2(0, 0); if (Content is FrameworkElement contentFE) { newSize = new Vector2((float)contentFE.ActualWidth, (float)contentFE.ActualHeight); } _shadowVisual.Size = newSize; } }
更一個主要的處理方法是 UpdateShadowMask(),負責更新投影的 mask:
根據須要設置投影的 Content 類型,Image,Shape,TextBlock 或 ImageExBase,使用 GetAlphaMask() 來獲得 alphaMask,賦值給 dropShadow 的 mask 屬性,做爲投影效果的顯示效果;配合上面的 UpdateShadowSize() 方法,以及 Opacity,Offset 等屬性,正確顯示投影效果;
private void UpdateShadowMask() { if (!IsSupported) { return; } if (Content != null) { CompositionBrush mask = null; if (Content is Image) { mask = ((Image)Content).GetAlphaMask(); } else if (Content is Shape) { mask = ((Shape)Content).GetAlphaMask(); } else if (Content is TextBlock) { mask = ((TextBlock)Content).GetAlphaMask(); } else if (Content is ImageExBase imageExBase) { imageExBase.ImageExInitialized += ImageExInitialized; if (imageExBase.IsInitialized) { imageExBase.ImageExInitialized -= ImageExInitialized; mask = ((ImageExBase)Content).GetAlphaMask(); } } _dropShadow.Mask = mask; } else { _dropShadow.Mask = null; } }
3. DropShadowPanel.xaml
DropShadowPanel 控件的樣式文件,咱們來看 Template 部分:投影效果的實現,是使用一個 Border 放置在實際內容控件的後面,經過 Border 中對內容的顯示和位置調整來實現投影。
<Style TargetType="controls:DropShadowPanel"> <Setter Property="IsTabStop" Value="False" /> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Stretch"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="controls:DropShadowPanel"> <Grid Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> <Border x:Name="ShadowElement" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
調用示例
下面的示例中,咱們定義了兩個相同的 TextBlock,其中一個應用了 DropShadow,一個未應用,能夠看出,應用了 DropShadow 的一個,文本明顯會有模糊的黑色陰影;
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Orientation="Vertical" VerticalAlignment="Center"> <controls:DropShadowPanel BlurRadius="10" ShadowOpacity="0.8" OffsetX="0" OffsetY="0" Color="Black" VerticalAlignment="Center" HorizontalAlignment="Center"> <TextBlock FontSize="20" TextWrapping="Wrap"> The DropShadowPanel Control allows the creation of a drop shadow effect for any Xaml FrameworkElement in the markup. You can control the following property of the drop shadow effect : Offset, Color, Opactity and Blur Radius. NOTE: Windows Anniversary Update (10.0.14393.0) is needed to support correctly this effect. </TextBlock> </controls:DropShadowPanel> <TextBlock TextWrapping="Wrap" FontSize="20" Margin="0,10,0,0"> The DropShadowPanel Control allows the creation of a drop shadow effect for any Xaml FrameworkElement in the markup. You can control the following property of the drop shadow effect : Offset, Color, Opactity and Blur Radius. NOTE: Windows Anniversary Update (10.0.14393.0) is needed to support correctly this effect. </TextBlock> </StackPanel> </Grid>
總結
到這裏咱們就把 UWP Community Toolkit 中的 DropShadowPanel 控件的源代碼實現過程和簡單的調用示例講解完成了,但願能對你們更好的理解和使用這個控件有所幫助。歡迎你們多多交流,謝謝!
最後,再跟你們安利一下 UWPCommunityToolkit 的官方微博:https://weibo.com/u/6506046490, 你們能夠經過微博關注最新動態。
衷心感謝 UWPCommunityToolkit 的做者們傑出的工做,Thank you so much, UWPCommunityToolkit authors!!!